in custom file types, don't complete keywords in literals or comments (IDEA-117226)

This commit is contained in:
Dmitry Jemerov
2013-12-09 20:02:52 +01:00
committed by peter
parent a3ec15e907
commit 84ad379adb
3 changed files with 27 additions and 0 deletions
@@ -0,0 +1 @@
// ca<caret>
@@ -37,6 +37,11 @@ public class CustomFileTypeCompletionTest extends LightCompletionTestCase {
testByCount(2, "case", "catch");
}
public void testComment() throws Throwable {
configureByFile(BASE_PATH + "foo.cs");
testByCount(0, new String[] { null });
}
public void testPlainTextSubstitution() throws IOException {
FileTypeManagerEx.getInstanceEx().registerFileType(MockLanguageFileType.INSTANCE, "xxx");
try {
@@ -17,8 +17,12 @@ package com.intellij.codeInsight.completion;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.ide.highlighter.custom.SyntaxTable;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
import com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType;
import com.intellij.psi.CustomHighlighterTokenType;
import com.intellij.psi.PsiElement;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.ProcessingContext;
import org.jetbrains.annotations.NotNull;
@@ -39,6 +43,10 @@ public class CustomFileTypeCompletionContributor extends CompletionContributor {
protected void addCompletions(@NotNull CompletionParameters parameters,
ProcessingContext context,
@NotNull CompletionResultSet result) {
if (inCommentOrLiteral(parameters)) {
return;
}
CustomSyntaxTableFileType fileType = (CustomSyntaxTableFileType)parameters.getOriginalFile().getFileType();
SyntaxTable syntaxTable = fileType.getSyntaxTable();
String prefix = findPrefix(parameters.getPosition(), parameters.getOffset());
@@ -52,6 +60,19 @@ public class CustomFileTypeCompletionContributor extends CompletionContributor {
});
}
private static boolean inCommentOrLiteral(CompletionParameters parameters) {
HighlighterIterator iterator = ((EditorEx)parameters.getEditor()).getHighlighter().createIterator(parameters.getOffset());
IElementType elementType = iterator.getTokenType();
if (elementType == CustomHighlighterTokenType.WHITESPACE) {
iterator.retreat();
elementType = iterator.getTokenType();
}
return elementType == CustomHighlighterTokenType.LINE_COMMENT ||
elementType == CustomHighlighterTokenType.MULTI_LINE_COMMENT ||
elementType == CustomHighlighterTokenType.STRING ||
elementType == CustomHighlighterTokenType.SINGLE_QUOTED_STRING;
}
private static void addVariants(CompletionResultSet resultSet, Set<String> keywords) {
for (String keyword : keywords) {
resultSet.addElement(LookupElementBuilder.create(keyword));