Java Code Examples for com.intellij.codeInsight.completion.CompletionResultSet#withPrefixMatcher()

The following examples show how to use com.intellij.codeInsight.completion.CompletionResultSet#withPrefixMatcher() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StepCompletionProvider.java    From Intellij-Plugin with Apache License 2.0 6 votes vote down vote up
@Override
public void addCompletions(@NotNull final CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet resultSet) {
    resultSet.stopHere();
    final String prefix = getPrefix(parameters);

    resultSet = resultSet.withPrefixMatcher(new GaugePrefixMatcher(prefix));
    Module moduleForPsiElement = GaugeUtil.moduleForPsiElement(parameters.getPosition());
    if (moduleForPsiElement == null) {
        return;
    }
    for (Type item : getStepsInModule(moduleForPsiElement)) {
        LookupElementBuilder element = LookupElementBuilder.create(item.getText()).withTypeText(item.getType(), true);
        element = element.withInsertHandler((InsertionContext context1, LookupElement item1) -> {
            if (context1.getCompletionChar() == '\t') {
                context1.getDocument().insertString(context1.getEditor().getCaretModel().getOffset(), "\n");
                PsiDocumentManager.getInstance(context1.getProject()).commitDocument(context1.getDocument());
            }
            PsiElement stepElement = context1.getFile().findElementAt(context1.getStartOffset()).getParent();
            final TemplateBuilder templateBuilder = TemplateBuilderFactory.getInstance().createTemplateBuilder(stepElement);
            replaceStepParamElements(prefix, context1, stepElement, templateBuilder);
            templateBuilder.run(context1.getEditor(), false);
        });
        resultSet.addElement(element);
    }
}
 
Example 2
Source File: StaticArgCompletionProvider.java    From Intellij-Plugin with Apache License 2.0 6 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet resultSet) {
    String prefix = getPrefix(parameters);
    resultSet = resultSet.withPrefixMatcher(new PlainPrefixMatcher(prefix));
    PsiFile specFile = parameters.getOriginalFile();
    SpecDetail specDetail = PsiTreeUtil.getChildOfType(specFile, SpecDetail.class);
    List<SpecStep> stepsInFile = new ArrayList<>();
    addContextSteps(specDetail, stepsInFile);
    addStepsInScenarios(specFile, stepsInFile);

    Set<String> staticArgs = getArgsFromSteps(stepsInFile);
    for (String arg : staticArgs) {
        if (arg != null) {
            LookupElementBuilder item = LookupElementBuilder.create(arg);
            resultSet.addElement(item);
        }
    }
}
 
Example 3
Source File: AnnotationExpressionGotoCompletionRegistrar.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Override
public void getLookupElements(@NotNull GotoCompletionProviderLookupArguments arguments) {
    final CompletionResultSet resultSet = arguments.getResultSet();
    String blockNamePrefix = resultSet.getPrefixMatcher().getPrefix();

    // find caret position:
    // - "has_role('"
    // - "has_role('YAML_ROLE_"
    if(!blockNamePrefix.matches("^.*(has_role|is_granted)\\s*\\(\\s*'[\\w-]*$")) {
        return;
    }

    // clear prefix caret string; for a clean completion independent from inside content
    CompletionResultSet myResultSet = resultSet.withPrefixMatcher("");

    VoterUtil.LookupElementPairConsumer consumer = new VoterUtil.LookupElementPairConsumer();
    VoterUtil.visitAttribute(getProject(), consumer);
    myResultSet.addAllElements(consumer.getLookupElements());
}
 
Example 4
Source File: ElmModuleCompletionProvider.java    From elm-plugin with MIT License 5 votes vote down vote up
void addCompletions(Project project, String prefix, CompletionResultSet resultSet) {
    String matcherPrefix = resultSet.getPrefixMatcher().getPrefix();
    int dotIndex = matcherPrefix.lastIndexOf('.');
    CompletionResultSet newResultSet = dotIndex < 0
            ? resultSet
            : resultSet.withPrefixMatcher(matcherPrefix.substring(dotIndex + 1));

    ElmModuleIndex.getAllModuleNames(project).stream()
            .map(s -> getModulePart(s, prefix))
            .forEach(optionalString -> optionalString.ifPresent(s -> addStringToResult(s, newResultSet)));
}
 
Example 5
Source File: DynamicPathCompletionProvider.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@Override
protected void addBashCompletions(String currentText, CompletionParameters parameters, ProcessingContext context, CompletionResultSet result) {
    //if we are in a combined word, get it
    PsiElement parentElement = parameters.getPosition().getParent();
    if (parentElement instanceof BashWord) {
        currentText = findCurrentText(parameters, parentElement);
    }

    result = result.withPrefixMatcher(currentText);

    String usedPrefix = findUsedPrefix(currentText);
    if (usedPrefix == null) {
        return;
    }

    //fixme shouldn't be needed
    String baseDir = findBaseDir(parameters, usedPrefix);
    if (baseDir == null) {
        return;
    }

    String relativePath = currentText.substring(usedPrefix.length());
    if (relativePath.startsWith("/")) {
        relativePath = relativePath.substring(1);
    }

    List<String> completions = CompletionUtil.completeRelativePath(baseDir, usedPrefix, relativePath);
    result.addAllElements(CompletionProviderUtils.createPathItems(completions));
}
 
Example 6
Source File: DynamicArgCompletionProvider.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
public void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet resultSet) {
    String prefix = getPrefix(parameters);
    resultSet = resultSet.withPrefixMatcher(new PlainPrefixMatcher(prefix));
    SpecDetail specDetail = PsiTreeUtil.getChildOfType(parameters.getOriginalFile(), SpecDetail.class);
    if (specDetail == null)
        return;
    SpecTable table = specDetail.getDataTable();
    if (table != null) {
        List<String> headers = table.getTableHeader().getHeaders();
        for (String header : headers) {
            LookupElementBuilder item = LookupElementBuilder.create(header);
            resultSet.addElement(item);
        }
    }
}
 
Example 7
Source File: ConceptStaticArgCompletionProvider.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected void addCompletions(CompletionParameters parameters, ProcessingContext processingContext, CompletionResultSet resultSet) {
    String prefix = getPrefix(parameters);
    resultSet = resultSet.withPrefixMatcher(new PlainPrefixMatcher(prefix));
    Collection<ConceptStaticArg> staticArgs = PsiTreeUtil.collectElementsOfType(parameters.getOriginalFile(), ConceptStaticArg.class);
    for (ConceptStaticArg arg : staticArgs) {
        if (arg != null) {
            String text = arg.getText().replaceFirst("\"", "");
            String textWithoutQuotes = text.substring(0, text.length() - 1);
            if (!textWithoutQuotes.equals(""))
                resultSet.addElement(LookupElementBuilder.create(textWithoutQuotes));
        }
    }
}
 
Example 8
Source File: ConceptDynamicArgCompletionProvider.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
public void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet resultSet) {
    String prefix = getPrefix(parameters);
    resultSet = resultSet.withPrefixMatcher(new PlainPrefixMatcher(prefix));
    Collection<ConceptDynamicArg> args = PsiTreeUtil.collectElementsOfType(parameters.getOriginalFile(), ConceptDynamicArg.class);
    for (ConceptDynamicArg arg : args) {
        LookupElementBuilder item = LookupElementBuilder.create(arg.getText().replaceAll("<|>", ""));
        resultSet.addElement(item);
    }
}
 
Example 9
Source File: YamlKeywordsCompletionProvider.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    PsiElement psiElement = parameters.getPosition();

    if (psiElement instanceof LeafPsiElement) {
        // Don't complete after tag (at end of line)
        // key: !my_tag <caret>\n
        if (YamlHelper.isElementAfterYamlTag(psiElement)) {
            return;
        }

        // Don't complete after End Of Line:
        // key: foo\n
        // <caret>
        if (YamlHelper.isElementAfterEol(psiElement)) {
            return;
        }

        String prefix = psiElement.getText();

        if (prefix.contains(CompletionUtil.DUMMY_IDENTIFIER_TRIMMED)) {
            prefix = prefix.substring(0, prefix.indexOf(CompletionUtil.DUMMY_IDENTIFIER_TRIMMED));
        }

        result = result.withPrefixMatcher(prefix);
    }

    for (Map.Entry<String, String> entry : YAML_KEYWORDS.entrySet()) {
        String yamlKeyword = entry.getKey();
        String yamlType = entry.getValue();

        LookupElementBuilder lookupElement = LookupElementBuilder.create(yamlKeyword)
                .withTypeText(yamlType);

        result.addElement(lookupElement);
    }
}
 
Example 10
Source File: CompletionContributorForTextField.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
@Override
public void fillCompletionVariants(CompletionParameters parameters, CompletionResultSet result) {
  PsiFile file = parameters.getOriginalFile();
  if (!(file instanceof PsiPlainTextFile)) return;

  TextFieldCompletionProvider field = file.getUserData(TextFieldCompletionProvider.COMPLETING_TEXT_FIELD_KEY);
  if (field == null) return;

  if (!(field instanceof DumbAware) && DumbService.isDumb(file.getProject())) return;
  
  String text = file.getText();
  int offset = Math.min(text.length(), parameters.getOffset());

  String prefix = field.getPrefix(text.substring(0, offset));

  CompletionResultSet activeResult;

  if (!result.getPrefixMatcher().getPrefix().equals(prefix)) {
    activeResult = result.withPrefixMatcher(prefix);
  }
  else {
    activeResult = result;
  }

  if (field.isCaseInsensitivity()) {
    activeResult = activeResult.caseInsensitive();
  }

  field.addCompletionVariants(text, offset, prefix, activeResult);
}