Added smart Ctrl+W support in Kotlin plugin.

#KT-1267 fixed
 #KT-1271 fixed
This commit is contained in:
Evgeny Gerashchenko
2012-02-24 19:05:34 +04:00
parent 230afd367f
commit 0bcdb9a95e
3 changed files with 208 additions and 0 deletions
+3
View File
@@ -120,6 +120,9 @@
<annotator language="jet" implementationClass="org.jetbrains.jet.plugin.annotations.JetPsiChecker"/>
<annotator language="jet" implementationClass="org.jetbrains.jet.plugin.annotations.DebugInfoAnnotator"/>
<extendWordSelectionHandler implementation="org.jetbrains.jet.plugin.JetStatementGroupSelectioner"/>
<extendWordSelectionHandler implementation="org.jetbrains.jet.plugin.JetCodeBlockSelectioner"/>
<documentationProvider implementation="org.jetbrains.jet.plugin.JetQuickDocumentationProvider"/>
<configurationType implementation="org.jetbrains.jet.plugin.run.JetRunConfigurationType"/>
<configurationProducer implementation="org.jetbrains.jet.plugin.run.JetRunConfigurationProducer"/>
@@ -0,0 +1,93 @@
/*
* 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 org.jetbrains.jet.plugin;
import com.intellij.codeInsight.editorActions.wordSelection.BasicSelectioner;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.impl.source.tree.LeafPsiElement;
import org.jetbrains.jet.lang.psi.JetBlockExpression;
import org.jetbrains.jet.lang.psi.JetWhenExpression;
import org.jetbrains.jet.lexer.JetTokens;
import java.util.ArrayList;
import java.util.List;
/**
* Originally from IDEA platform: CodeBlockOrInitializerSelectioner
*/
public class JetCodeBlockSelectioner extends BasicSelectioner {
public boolean canSelect(PsiElement e) {
return e instanceof JetBlockExpression || e instanceof JetWhenExpression;
}
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
List<TextRange> result = new ArrayList<TextRange>();
ASTNode[] children = e.getNode().getChildren(null);
int start = findOpeningBrace(children);
int end = findClosingBrace(children, start);
result.add(e.getTextRange());
result.addAll(expandToWholeLine(editorText, new TextRange(start, end)));
return result;
}
public static int findOpeningBrace(ASTNode[] children) {
int start = children[children.length - 1].getTextRange().getStartOffset();
for (int i = 0; i < children.length; i++) {
PsiElement child = children[i].getPsi();
if (child instanceof LeafPsiElement) {
if (((LeafPsiElement) child).getElementType() == JetTokens.LBRACE) {
int j = i + 1;
while (children[j] instanceof PsiWhiteSpace) {
j++;
}
start = children[j].getTextRange().getStartOffset();
}
}
}
return start;
}
public static int findClosingBrace(ASTNode[] children, int startOffset) {
int end = children[children.length - 1].getTextRange().getEndOffset();
for (int i = 0; i < children.length; i++) {
PsiElement child = children[i].getPsi();
if (child instanceof LeafPsiElement) {
if (((LeafPsiElement) child).getElementType() == JetTokens.RBRACE) {
int j = i - 1;
while (children[j] instanceof PsiWhiteSpace && children[j].getTextRange().getStartOffset() > startOffset) {
j--;
}
end = children[j].getTextRange().getEndOffset();
}
}
}
return end;
}
}
@@ -0,0 +1,112 @@
/*
* 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 org.jetbrains.jet.plugin;
import com.intellij.codeInsight.editorActions.wordSelection.BasicSelectioner;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.LineTokenizer;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.impl.source.tree.LeafPsiElement;
import org.jetbrains.jet.lang.psi.JetBlockExpression;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetWhenEntry;
import org.jetbrains.jet.lang.psi.JetWhenExpression;
import org.jetbrains.jet.lexer.JetTokens;
import java.util.ArrayList;
import java.util.List;
/**
* Originally from IDEA platform: StatementGroupSelectioner
*/
public class JetStatementGroupSelectioner extends BasicSelectioner {
public boolean canSelect(PsiElement e) {
return e instanceof JetExpression || e instanceof JetWhenEntry;
}
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
List<TextRange> result = new ArrayList<TextRange>();
PsiElement parent = e.getParent();
if (!(parent instanceof JetBlockExpression) && !(parent instanceof JetWhenExpression)) {
return result;
}
PsiElement startElement = e;
PsiElement endElement = e;
while (startElement.getPrevSibling() != null) {
PsiElement sibling = startElement.getPrevSibling();
if (sibling instanceof LeafPsiElement) {
if (((LeafPsiElement) sibling).getElementType() == JetTokens.LBRACE) {
break;
}
}
if (sibling instanceof PsiWhiteSpace) {
PsiWhiteSpace whiteSpace = (PsiWhiteSpace) sibling;
String[] strings = LineTokenizer.tokenize(whiteSpace.getText().toCharArray(), false);
if (strings.length > 2) {
break;
}
}
startElement = sibling;
}
while (startElement instanceof PsiWhiteSpace) {
startElement = startElement.getNextSibling();
}
while (endElement.getNextSibling() != null) {
PsiElement sibling = endElement.getNextSibling();
if (sibling instanceof LeafPsiElement) {
if (((LeafPsiElement) sibling).getElementType() == JetTokens.RBRACE) {
break;
}
}
if (sibling instanceof PsiWhiteSpace) {
PsiWhiteSpace whiteSpace = (PsiWhiteSpace) sibling;
String[] strings = LineTokenizer.tokenize(whiteSpace.getText().toCharArray(), false);
if (strings.length > 2) {
break;
}
}
endElement = sibling;
}
while (endElement instanceof PsiWhiteSpace) {
endElement = endElement.getPrevSibling();
}
result.addAll(expandToWholeLine(editorText, new TextRange(startElement.getTextRange().getStartOffset(),
endElement.getTextRange().getEndOffset())));
return result;
}
}