mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
custom file type completion rewritten as completion contributor
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package com.intellij.patterns;
|
||||
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiDirectory;
|
||||
import com.intellij.psi.PsiFile;
|
||||
@@ -63,6 +64,15 @@ public class PsiFilePattern<T extends PsiFile, Self extends PsiFilePattern<T, Se
|
||||
});
|
||||
}
|
||||
|
||||
public Self withFileType(final ElementPattern<? extends FileType> fileTypePattern) {
|
||||
return with(new PatternCondition<T>("withFileType") {
|
||||
@Override
|
||||
public boolean accepts(@NotNull T file, ProcessingContext context) {
|
||||
return fileTypePattern.accepts(file.getFileType(), context);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static class Capture<T extends PsiFile> extends PsiFilePattern<T,Capture<T>> {
|
||||
|
||||
protected Capture(final Class<T> aClass) {
|
||||
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2000-2013 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.completion;
|
||||
|
||||
import com.intellij.codeInsight.lookup.LookupElementBuilder;
|
||||
import com.intellij.ide.highlighter.custom.SyntaxTable;
|
||||
import com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import static com.intellij.patterns.PlatformPatterns.psiElement;
|
||||
import static com.intellij.patterns.PlatformPatterns.psiFile;
|
||||
import static com.intellij.patterns.StandardPatterns.instanceOf;
|
||||
|
||||
/**
|
||||
* @author yole
|
||||
*/
|
||||
public class CustomFileTypeCompletionContributor extends CompletionContributor {
|
||||
public CustomFileTypeCompletionContributor() {
|
||||
extend(CompletionType.BASIC, psiElement().inFile(psiFile().withFileType(instanceOf(CustomSyntaxTableFileType.class))),
|
||||
new CompletionProvider<CompletionParameters>() {
|
||||
@Override
|
||||
protected void addCompletions(@NotNull CompletionParameters parameters,
|
||||
ProcessingContext context,
|
||||
@NotNull CompletionResultSet result) {
|
||||
CustomSyntaxTableFileType fileType = (CustomSyntaxTableFileType)parameters.getOriginalFile().getFileType();
|
||||
SyntaxTable syntaxTable = fileType.getSyntaxTable();
|
||||
String prefix = findPrefix(parameters.getPosition(), parameters.getOffset());
|
||||
CompletionResultSet resultSetWithPrefix = result.withPrefixMatcher(prefix);
|
||||
|
||||
addVariants(resultSetWithPrefix, syntaxTable.getKeywords1());
|
||||
addVariants(resultSetWithPrefix, syntaxTable.getKeywords2());
|
||||
addVariants(resultSetWithPrefix, syntaxTable.getKeywords3());
|
||||
addVariants(resultSetWithPrefix, syntaxTable.getKeywords4());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void addVariants(CompletionResultSet resultSet, Set<String> keywords) {
|
||||
for (String keyword : keywords) {
|
||||
resultSet.addElement(LookupElementBuilder.create(keyword));
|
||||
}
|
||||
}
|
||||
|
||||
private static String findPrefix(PsiElement insertedElement, int offset) {
|
||||
String text = insertedElement.getText();
|
||||
int offsetInElement = offset - insertedElement.getTextOffset();
|
||||
int start = offsetInElement - 1;
|
||||
while(start >=0 ) {
|
||||
if(!Character.isJavaIdentifierStart(text.charAt(start))) break;
|
||||
--start;
|
||||
}
|
||||
return text.substring(start+1, offsetInElement).trim();
|
||||
}
|
||||
|
||||
}
|
||||
-58
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2009 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.completion;
|
||||
|
||||
import com.intellij.codeInsight.TailType;
|
||||
import com.intellij.ide.highlighter.custom.SyntaxTable;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.filters.TrueFilter;
|
||||
|
||||
/**
|
||||
* @author Maxim.Mossienko
|
||||
*/
|
||||
public class SyntaxTableCompletionData extends CompletionData{
|
||||
private final SyntaxTable mySyntaxTable;
|
||||
|
||||
public SyntaxTableCompletionData(SyntaxTable _syntaxTable) {
|
||||
mySyntaxTable = _syntaxTable;
|
||||
mySyntaxTable.getKeywords1();
|
||||
|
||||
final CompletionVariant variant = new CompletionVariant(TrueFilter.INSTANCE);
|
||||
variant.includeScopeClass(PsiElement.class, true);
|
||||
variant.addCompletionFilter(TrueFilter.INSTANCE);
|
||||
final String[] empty = {};
|
||||
|
||||
variant.addCompletion(mySyntaxTable.getKeywords1().toArray(empty), TailType.NONE);
|
||||
variant.addCompletion(mySyntaxTable.getKeywords2().toArray(empty), TailType.NONE);
|
||||
variant.addCompletion(mySyntaxTable.getKeywords3().toArray(empty), TailType.NONE);
|
||||
variant.addCompletion(mySyntaxTable.getKeywords4().toArray(empty), TailType.NONE);
|
||||
|
||||
registerVariant(variant);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String findPrefix(PsiElement insertedElement, int offset) {
|
||||
String text = insertedElement.getText();
|
||||
int offsetInElement = offset - insertedElement.getTextOffset();
|
||||
int start = offsetInElement - 1;
|
||||
while(start >=0 ) {
|
||||
if(!Character.isJavaIdentifierStart(text.charAt(start))) break;
|
||||
--start;
|
||||
}
|
||||
return text.substring(start+1, offsetInElement).trim();
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -138,7 +138,7 @@ public class WordCompletionContributor extends CompletionContributor implements
|
||||
|
||||
final PsiFile file = insertedElement.getContainingFile();
|
||||
final CompletionData data = CompletionUtil.getCompletionDataByElement(insertedElement, file);
|
||||
if (data != null && !(data instanceof SyntaxTableCompletionData)) {
|
||||
if (data != null) {
|
||||
Set<CompletionVariant> toAdd = new HashSet<CompletionVariant>();
|
||||
data.addKeywordVariants(toAdd, insertedElement, file);
|
||||
for (CompletionVariant completionVariant : toAdd) {
|
||||
|
||||
-3
@@ -16,8 +16,6 @@
|
||||
|
||||
package com.intellij.ide.highlighter.custom.impl;
|
||||
|
||||
import com.intellij.codeInsight.completion.CompletionUtil;
|
||||
import com.intellij.codeInsight.completion.SyntaxTableCompletionData;
|
||||
import com.intellij.codeInsight.editorActions.TypedHandler;
|
||||
import com.intellij.codeInsight.highlighting.BraceMatchingUtil;
|
||||
import com.intellij.ide.highlighter.FileTypeRegistrator;
|
||||
@@ -37,7 +35,6 @@ public class StandardFileTypeRegistrator implements FileTypeRegistrator {
|
||||
|
||||
private static void init(final AbstractFileType abstractFileType) {
|
||||
SyntaxTable table = abstractFileType.getSyntaxTable();
|
||||
CompletionUtil.registerCompletionData(abstractFileType,new SyntaxTableCompletionData(table));
|
||||
|
||||
if (!isEmpty(table.getStartComment()) && !isEmpty(table.getEndComment()) ||
|
||||
!isEmpty(table.getLineComment())) {
|
||||
|
||||
@@ -838,6 +838,8 @@
|
||||
<completion.contributor language="TEXT" implementationClass="com.intellij.util.CompletionContributorForTextField"
|
||||
order="first, before commitCompletion"/>
|
||||
|
||||
<completion.contributor language="any" implementationClass="com.intellij.codeInsight.completion.CustomFileTypeCompletionContributor"/>
|
||||
|
||||
<pathMacroFilter implementation="com.intellij.execution.configuration.RunConfigurationPathMacroFilter"/>
|
||||
<pathMacroFilter implementation="com.intellij.openapi.application.StructuralSearchPathMacroFilter"/>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user