use of com.intellij.codeInsight.completion.CompletionParameters in project intellij-plugins by JetBrains.
the class LanguageListCompletionTest method testCustomCompletionProvider.
public void testCustomCompletionProvider() {
try {
PlatformTestUtil.registerExtension(CodeFenceLanguageProvider.EP_NAME, new CodeFenceLanguageProvider() {
@Nullable
@Override
public Language getLanguageByInfoString(@NotNull String infoString) {
return null;
}
@NotNull
@Override
public List<LookupElement> getCompletionVariantsForInfoString(@NotNull CompletionParameters parameters) {
return Collections.singletonList(LookupElementBuilder.create("{js is a great ecma}").withInsertHandler((context, item) -> {
context.getDocument().insertString(context.getEditor().getCaretModel().getOffset(), "Customized insertion");
context.getEditor().getCaretModel().moveCaretRelatively("Customized insertion".length(), 0, true, false, true);
}));
}
}, myFixture.getTestRootDisposable());
LanguageGuesser.INSTANCE.resetCodeFenceLanguageProviders();
configure();
myFixture.completeBasic();
assertContainsElements(myFixture.getLookupElementStrings(), "js", "javascript", "{js is a great ecma}");
myFixture.type("ecm\t");
checkResult();
} finally {
LanguageGuesser.INSTANCE.resetCodeFenceLanguageProviders();
}
}
use of com.intellij.codeInsight.completion.CompletionParameters in project oxy-template-support-plugin by mutant-industries.
the class JavaContext method fillCompletionVariants.
@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
Module module;
PsiElement element = parameters.getOriginalFile().getViewProvider().findElementAt(parameters.getOffset() - 1, OxyTemplateInnerJs.INSTANCE);
if (element == null || element.getNode().getElementType() != JSTokenTypes.DOT && (element = element.getPrevSibling()) == null) {
return;
}
if (element.getNode().getElementType() == TokenType.WHITE_SPACE) {
element = element.getPrevSibling();
}
while (element != null && element.getNode().getElementType() != JSTokenTypes.DOT) {
element = element.getLastChild();
if (element instanceof PsiErrorElement) {
element = element.getPrevSibling();
}
}
if (element == null || element.getNode().getElementType() != JSTokenTypes.DOT || (element = element.getPrevSibling()) == null) {
return;
}
if (element.getNode().getElementType() == TokenType.WHITE_SPACE) {
element = element.getPrevSibling();
}
if (!(element instanceof JSExpression) || (module = ModuleUtilCore.findModuleForPsiElement(parameters.getOriginalFile())) == null) {
return;
}
JSSimpleTypeProcessor typeProcessor = new JSSimpleTypeProcessor();
JSTypeEvaluator.evaluateTypes((JSExpression) element, parameters.getOriginalFile().getViewProvider().getPsi(OxyTemplateInnerJs.INSTANCE), typeProcessor);
JSType type = typeProcessor.getType();
List<String> possibleJavaTypes = new LinkedList<>();
if (type != null) {
if (type instanceof JSCompositeTypeImpl) {
possibleJavaTypes.addAll(((JSCompositeTypeImpl) type).getTypes().stream().filter(jsType -> jsType instanceof JSTypeImpl).map(JSType::getTypeText).collect(Collectors.toList()));
} else if (type instanceof JSTypeImpl) {
possibleJavaTypes.add(type.getTypeText());
}
}
JavaPsiFacade facade = JavaPsiFacade.getInstance(parameters.getOriginalFile().getProject());
GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module);
List<PsiClass> possibleInstancesOf = new LinkedList<>();
boolean suggestionsFound = false;
PsiClass aClass;
for (String possibleType : possibleJavaTypes) {
if ((aClass = facade.findClass(possibleType, scope)) != null) {
possibleInstancesOf.add(aClass);
possibleInstancesOf.addAll(ExtenderProvider.getExtenders(aClass));
}
}
for (PsiClass psiClass : possibleInstancesOf) {
List<String> alreadySuggested = new LinkedList<>();
for (PsiMethod method : psiClass.getAllMethods()) {
if (method.getContainingClass() == null || Object.class.getName().equals(method.getContainingClass().getQualifiedName()) || method.getReturnType() == null || shippedBaseExtenderMethods.contains(method.getName()) || !method.getModifierList().hasModifierProperty(PsiModifier.PUBLIC) || InheritanceUtil.isInheritor(method.getContainingClass(), EXTENDER_INTERFACE_FQN) && !InheritanceUtil.isInheritor(method.getContainingClass(), EXTENDER_BASE_CLASS_FQN)) {
continue;
}
String insertText = method.getName();
String presentableText = method.getName();
if (insertText.matches("((^is)|(^get)|(^set))[A-Z].*")) {
insertText = presentableText = StringUtils.decapitalize(insertText.replaceFirst("(^is)|(^get)|(^set)", ""));
} else {
presentableText = method.getPresentation() == null ? presentableText + "()" : method.getPresentation().getPresentableText();
insertText += "()";
}
if (alreadySuggested.contains(insertText)) {
continue;
}
result.consume(LookupElementBuilder.create(method, insertText).withIcon(method.getIcon(0)).withTypeText(method.getReturnType().getPresentableText(), true).withTailText(" (" + psiClass.getContainingFile().getName() + ")", true).withPresentableText(presentableText).withInsertHandler(new TrailingPatternConsumer(INSERT_CONSUME)).withAutoCompletionPolicy(AutoCompletionPolicy.GIVE_CHANCE_TO_OVERWRITE));
alreadySuggested.add(insertText);
suggestionsFound = true;
}
}
if (suggestionsFound) {
result.stopHere();
}
}
use of com.intellij.codeInsight.completion.CompletionParameters in project component-runtime by Talend.
the class PropertiesCompletionProvider method addCompletions.
@Override
protected void addCompletions(final CompletionParameters completionParameters, final ProcessingContext processingContext, final CompletionResultSet resultSet) {
final PsiElement element = completionParameters.getPosition();
if (!LeafPsiElement.class.isInstance(element)) {
// ignore comment
return;
}
final Project project = element.getProject();
final Module module = findModule(element);
final SuggestionService service = ServiceManager.getService(project, SuggestionService.class);
if ((module == null || !service.isSupported(completionParameters))) {
// limit suggestion to Messages
return;
}
if (PropertyValueImpl.class.isInstance(element)) {
ofNullable(PropertyValueImpl.class.cast(element).getPrevSibling()).map(PsiElement::getPrevSibling).map(PsiElement::getText).ifPresent(text -> resultSet.addAllElements(service.computeValueSuggestions(text)));
} else if (PropertyKeyImpl.class.isInstance(element)) {
final List<String> containerElements = PropertiesFileImpl.class.cast(element.getContainingFile()).getProperties().stream().filter(p -> !Objects.equals(p.getKey(), element.getText())).map(IProperty::getKey).collect(toList());
resultSet.addAllElements(service.computeKeySuggestions(project, module, getPropertiesPackage(module, completionParameters), containerElements, truncateIdeaDummyIdentifier(element)));
}
}
use of com.intellij.codeInsight.completion.CompletionParameters in project android by JetBrains.
the class EditLogFilterDialog method createEditorFields.
private void createEditorFields() {
myNameFieldLabel.setLabelFor(myFilterNameField);
myLogMessageField = new RegexFilterComponent(LOG_FILTER_MESSAGE_HISTORY, FILTER_HISTORY_SIZE);
myLogMessageFieldWrapper.add(myLogMessageField);
myLogMessageLabel.setLabelFor(myLogMessageField);
myTagField = new RegexFilterComponent(LOG_FILTER_TAG_HISTORY, FILTER_HISTORY_SIZE);
myTagFieldWrapper.add(myTagField);
myLogTagLabel.setLabelFor(myTagField);
myPidField = new TextFieldWithAutoCompletion<>(myProject, new TextFieldWithAutoCompletion.StringsCompletionProvider(null, null) {
@NotNull
@Override
public Collection<String> getItems(String prefix, boolean cached, CompletionParameters parameters) {
parseExistingMessagesIfNecessary();
setItems(myUsedPids);
return super.getItems(prefix, cached, parameters);
}
@Override
public int compare(String item1, String item2) {
final int pid1 = Integer.parseInt(item1);
final int pid2 = Integer.parseInt(item2);
return Comparing.compare(pid1, pid2);
}
}, true, null);
myPidFieldWrapper.add(myPidField);
myPidLabel.setLabelFor(myPidField);
myPackageNameField = new RegexFilterComponent(LOG_FILTER_PACKAGE_NAME_HISTORY, FILTER_HISTORY_SIZE);
myPackageNameFieldWrapper.add(myPackageNameField);
myPackageNameLabel.setLabelFor(myPackageNameField);
myLogLevelCombo.setModel(new EnumComboBoxModel<>(Log.LogLevel.class));
myLogLevelCombo.setRenderer(new ListCellRendererWrapper() {
@Override
public void customize(JList list, Object value, int index, boolean selected, boolean hasFocus) {
if (value != null) {
setText(StringUtil.capitalize(((Log.LogLevel) value).getStringValue().toLowerCase()));
}
}
});
myLogLevelCombo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (myActiveFilter == null) {
return;
}
Log.LogLevel selectedItem = (Log.LogLevel) myLogLevelCombo.getSelectedItem();
myActiveFilter.setLogLevel(selectedItem.getStringValue());
}
});
final Key<JComponent> componentKey = new Key<>("myComponent");
myFilterNameField.getDocument().putUserData(componentKey, myFilterNameField);
myPidField.getDocument().putUserData(componentKey, myPidField);
DocumentListener l = new DocumentListener() {
@Override
public void documentChanged(DocumentEvent e) {
if (myActiveFilter == null) {
return;
}
String text = e.getDocument().getText().trim();
JComponent src = e.getDocument().getUserData(componentKey);
if (src == myPidField) {
myActiveFilter.setPid(text);
} else if (src == myFilterNameField) {
int index = myFiltersList.getSelectedIndex();
if (index != -1) {
myFiltersListModel.setElementAt(text, index);
}
myActiveFilter.setName(text);
}
}
};
myFilterNameField.getDocument().addDocumentListener(l);
myPidField.getDocument().addDocumentListener(l);
RegexFilterComponent.Listener rl = new RegexFilterComponent.Listener() {
@Override
public void filterChanged(RegexFilterComponent filter) {
if (myActiveFilter == null) {
return;
}
if (filter == myTagField) {
myActiveFilter.setLogTagPattern(filter.getFilter());
myActiveFilter.setLogTagIsRegex(filter.isRegex());
} else if (filter == myLogMessageField) {
myActiveFilter.setLogMessagePattern(filter.getFilter());
myActiveFilter.setLogMessageIsRegex(filter.isRegex());
} else if (filter == myPackageNameField) {
myActiveFilter.setPackageNamePattern(filter.getFilter());
myActiveFilter.setPackageNameIsRegex(filter.isRegex());
}
}
};
myTagField.addRegexListener(rl);
myLogMessageField.addRegexListener(rl);
myPackageNameField.addRegexListener(rl);
}
use of com.intellij.codeInsight.completion.CompletionParameters in project intellij-plugins by JetBrains.
the class CfmlTagNamesCompletionProvider method addCompletionsFromDirectory.
private void addCompletionsFromDirectory(CompletionResultSet result, CompletionParameters parameters, String libtag, String prefix) {
final PsiFile originalFile = parameters.getOriginalFile();
final VirtualFile folder = CfmlUtil.findFileByLibTag(originalFile, libtag);
if (folder != null && folder.isDirectory()) {
final Set<String> names = new THashSet<>(CfmlIndex.getInstance(originalFile.getProject()).getAllComponentsNames());
names.retainAll(ContainerUtil.map(folder.getChildren(), virtualFile -> FileUtil.getNameWithoutExtension(virtualFile.getName()).toLowerCase()));
for (String componentName : names) {
result.addElement(LookupElementBuilder.create(prefix + ':' + componentName).withCaseSensitivity(false));
}
}
}
Aggregations