select word to honor camel-humps in java string literals, treat $ as word separator

This commit is contained in:
peter
2012-01-01 23:14:31 +01:00
parent 620d54b647
commit 2841c3eb28
5 changed files with 85 additions and 28 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,17 +24,13 @@ import com.intellij.psi.javadoc.PsiDocTag;
*/
public class JavaBasicWordSelectionFilter implements Condition<PsiElement> {
public boolean value(final PsiElement e) {
if (e instanceof PsiJavaToken && ((PsiJavaToken)e).getTokenType() == JavaTokenType.IDENTIFIER) {
return true;
}
return !(e instanceof PsiCodeBlock) &&
!(e instanceof PsiArrayInitializerExpression) &&
!(e instanceof PsiParameterList) &&
!(e instanceof PsiExpressionList) &&
!(e instanceof PsiBlockStatement) &&
!(e instanceof PsiJavaCodeReferenceElement) &&
!(e instanceof PsiJavaToken &&
!(e instanceof PsiKeyword)) &&
!(e instanceof PsiJavaToken) &&
!(e instanceof PsiDocTag);
}
}
@@ -0,0 +1,64 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.editorActions.wordSelection;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiJavaToken;
import com.intellij.psi.PsiKeyword;
import com.intellij.psi.tree.IElementType;
import java.util.Iterator;
import java.util.List;
/**
* @author peter
*/
public class JavaWordSelectioner extends AbstractWordSelectioner {
@Override
public boolean canSelect(PsiElement e) {
if (e instanceof PsiKeyword) {
return true;
}
if (e instanceof PsiJavaToken) {
IElementType tokenType = ((PsiJavaToken)e).getTokenType();
return tokenType == JavaTokenType.IDENTIFIER || tokenType == JavaTokenType.STRING_LITERAL;
}
return false;
}
@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
List<TextRange> ranges = super.select(e, editorText, cursorOffset, editor);
if (e instanceof PsiJavaToken && ((PsiJavaToken)e).getTokenType() == JavaTokenType.STRING_LITERAL) {
killRangesBreakingEscapes(e, ranges, e.getTextRange());
}
return ranges;
}
private static void killRangesBreakingEscapes(PsiElement e, List<TextRange> ranges, TextRange literalRange) {
for (Iterator<TextRange> iterator = ranges.iterator(); iterator.hasNext(); ) {
TextRange each = iterator.next();
if (literalRange.contains(each) &&
literalRange.getStartOffset() < each.getStartOffset() &&
e.getText().charAt(each.getStartOffset() - literalRange.getStartOffset() - 1) == '\\') {
iterator.remove();
}
}
}
}
@@ -89,27 +89,11 @@ public class SelectWordUtil {
int end = cursorOffset + 1;
final int textLen = editorText.length();
while (start > 0 && Character.isJavaIdentifierPart(editorText.charAt(start - 1))) {
final char prevChar = editorText.charAt(start - 1);
final char curChar = editorText.charAt(start);
final char nextChar = start + 1 < textLen ? editorText.charAt(start + 1) : 0; // 0x00 is not lowercase.
if (Character.isLowerCase(prevChar) && Character.isUpperCase(curChar) || prevChar == '_' && curChar != '_' ||
Character.isUpperCase(prevChar) && Character.isUpperCase(curChar) && Character.isLowerCase(nextChar)) {
break;
}
while (start > 0 && Character.isJavaIdentifierPart(editorText.charAt(start - 1)) && !isWordBound(editorText, start)) {
start--;
}
while (end < textLen && Character.isJavaIdentifierPart(editorText.charAt(end))) {
final char prevChar = editorText.charAt(end - 1);
final char curChar = editorText.charAt(end);
final char nextChar = end + 1 < textLen ? editorText.charAt(end + 1) : 0; // 0x00 is not lowercase
if (Character.isLowerCase(prevChar) && Character.isUpperCase(curChar) || prevChar != '_' && curChar == '_' ||
Character.isUpperCase(prevChar) && Character.isUpperCase(curChar) && Character.isLowerCase(nextChar)) {
break;
}
while (end < textLen && Character.isJavaIdentifierPart(editorText.charAt(end)) && !isWordBound(editorText, end)) {
end++;
}
@@ -121,6 +105,19 @@ public class SelectWordUtil {
return null;
}
private static boolean isWordBound(CharSequence editorText, int offset) {
final char prevChar = editorText.charAt(offset - 1);
final char curChar = editorText.charAt(offset);
final char nextChar = offset + 1 < editorText.length() ? editorText.charAt(offset + 1) : 0; // 0x00 is not lowercase.
return Character.isLowerCase(prevChar) && Character.isUpperCase(curChar) ||
prevChar == '_' && curChar != '_' ||
prevChar != '_' && curChar == '_' ||
prevChar == '$' && Character.isLetterOrDigit(curChar) ||
Character.isLetterOrDigit(prevChar) && curChar == '$' ||
Character.isUpperCase(prevChar) && Character.isUpperCase(curChar) && Character.isLowerCase(nextChar);
}
@Nullable
public static TextRange getWordSelectionRange(@NotNull CharSequence editorText, int cursorOffset) {
int length = editorText.length();
@@ -17,14 +17,13 @@
package com.intellij.codeInsight.editorActions.wordSelection;
import com.google.common.collect.Lists;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import com.intellij.codeInsight.editorActions.ExtendWordSelectionHandlerBase;
import com.intellij.codeInsight.editorActions.SelectWordUtil;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import java.util.List;
import java.util.ArrayList;
/**
* @author yole
+1
View File
@@ -1125,6 +1125,7 @@
<templateCompletionProcessor implementation="com.intellij.codeInsight.template.macro.JavaTemplateCompletionProcessor"/>
<extendWordSelectionHandler implementation="com.intellij.codeInsight.editorActions.wordSelection.JavaWordSelectioner"/>
<basicWordSelectionFilter implementation="com.intellij.codeInsight.editorActions.wordSelection.JavaBasicWordSelectionFilter"/>
<fileIconPatcher implementation="com.intellij.ide.JavaFileIconPatcher"/>