com.intellij.codeInsight.completion.CodeCompletionHandlerBase Java Examples

The following examples show how to use com.intellij.codeInsight.completion.CodeCompletionHandlerBase. 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: IgnoreSyntaxEntryFix.java    From idea-gitignore with MIT License 6 votes vote down vote up
/**
 * Handles QuickFix action invoked on {@link IgnoreSyntax}.
 *
 * @param project      the {@link Project} containing the working file
 * @param file         the {@link PsiFile} containing handled entry
 * @param startElement the {@link IgnoreSyntax} that will be selected and replaced
 * @param endElement   the {@link PsiElement} which is ignored in invoked action
 */
@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file,
                   @Nullable("is null when called from inspection") Editor editor,
                   @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    if (startElement instanceof IgnoreSyntax) {
        PsiElement value = ((IgnoreSyntax) startElement).getValue();
        if (editor != null) {
            editor.getSelectionModel().setSelection(
                    value.getTextOffset(),
                    value.getTextOffset() + value.getTextLength()
            );
        }
        if (editor != null) {
            new CodeCompletionHandlerBase(CompletionType.BASIC).invokeCompletion(project, editor);
        }
    }
}
 
Example #2
Source File: AnnotationLightCodeInsightFixtureTestCase.java    From idea-php-generics-plugin with MIT License 5 votes vote down vote up
@Override
public void run() {
    CommandProcessor.getInstance().executeCommand(getProject(), () -> {
        final CodeCompletionHandlerBase handler = new CodeCompletionHandlerBase(CompletionType.BASIC) {

            @Override
            protected void completionFinished(final CompletionProgressIndicator indicator, boolean hasModifiers) {

                // find our lookup element
                final LookupElement lookupElement = ContainerUtil.find(indicator.getLookup().getItems(), insert::match);

                if(lookupElement == null) {
                    fail("No matching lookup element found");
                }

                // overwrite behavior and force completion + insertHandler
                CommandProcessor.getInstance().executeCommand(indicator.getProject(), new Runnable() {
                    @Override
                    public void run() {
                        //indicator.setMergeCommand(); Currently method has package level access
                        indicator.getLookup().finishLookup(Lookup.AUTO_INSERT_SELECT_CHAR, lookupElement);
                    }
                }, "Autocompletion", null);
            }
        };

        Editor editor = InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(getEditor(), getFile());
        handler.invokeCompletion(getProject(), editor);
        PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
    }, null, null);
}
 
Example #3
Source File: LSIncompleteCompletionProposal.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
public LSIncompleteCompletionProposal(Editor editor, int offset, CompletionItem item, LanguageServer languageServer) {
    this.item = item;
    this.editor = editor;
    this.languageServer = languageServer;
    this.initialOffset = offset;
    this.currentOffset = offset;
    this.bestOffset = getPrefixCompletionStart(editor.getDocument(), offset);
    putUserData(CodeCompletionHandlerBase.DIRECT_INSERTION, true);
}
 
Example #4
Source File: AnnotationLightCodeInsightFixtureTestCase.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@Override
public void run() {
    CommandProcessor.getInstance().executeCommand(getProject(), () -> {
        final CodeCompletionHandlerBase handler = new CodeCompletionHandlerBase(CompletionType.BASIC) {

            @Override
            protected void completionFinished(final CompletionProgressIndicator indicator, boolean hasModifiers) {

                // find our lookup element
                final LookupElement lookupElement = ContainerUtil.find(indicator.getLookup().getItems(), insert::match);

                if(lookupElement == null) {
                    fail("No matching lookup element found");
                }

                // overwrite behavior and force completion + insertHandler
                CommandProcessor.getInstance().executeCommand(indicator.getProject(), new Runnable() {
                    @Override
                    public void run() {
                        //indicator.setMergeCommand(); Currently method has package level access
                        indicator.getLookup().finishLookup(Lookup.AUTO_INSERT_SELECT_CHAR, lookupElement);
                    }
                }, "Autocompletion", null);
            }
        };

        Editor editor = InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(getEditor(), getFile());
        handler.invokeCompletion(getProject(), editor);
        PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
    }, null, null);
}
 
Example #5
Source File: CodeInsightTestFixtureImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public LookupElement[] complete(final CompletionType type, final int invocationCount) {
  assertInitialized();
  myEmptyLookup = false;
  UIUtil.invokeAndWaitIfNeeded(new Runnable() {
    @Override
    public void run() {
      CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
        @Override
        public void run() {
          final CodeCompletionHandlerBase handler = new CodeCompletionHandlerBase(type) {
            @Override
            protected void completionFinished(CompletionProgressIndicator indicator, boolean hasModifiers) {
              myEmptyLookup = indicator.getLookup().getItems().isEmpty();
              super.completionFinished(indicator, hasModifiers);
            }
          };
          Editor editor = getCompletionEditor();
          handler.invokeCompletion(getProject(), editor, invocationCount);
          PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); // to compare with file text
        }
      }, null, null);
    }
  });

  return getLookupElements();
}
 
Example #6
Source File: TargetFileCompletionIntegrationTest.java    From intellij-pants-plugin with Apache License 2.0 4 votes vote down vote up
private void completionTest(String stringToComplete, String[] expected) {
  String fullStringToComplete = "\n\n" + stringToComplete;
  // should be only tested with pants versions above 1.24.0
  if (PantsUtil.isCompatibleProjectPantsVersion(myProjectRoot.getPath(), "1.24.0")) {
    invalidateCaches();

    String helloProjectPath = "examples/src/scala/org/pantsbuild/example/hello/";
    doImport(helloProjectPath);
    VirtualFile vfile = myProjectRoot.findFileByRelativePath(helloProjectPath + "BUILD");
    assertNotNull(vfile);

    Document doc = FileDocumentManager.getInstance().getDocument(vfile);

    String originalContent = doc.getText();

    int offset = doc.getText().length() + fullStringToComplete.indexOf(CURSOR);

    append(doc, fullStringToComplete.replace(CURSOR, ""));
    assertNotNull(doc.getText());

    Editor editor = EditorFactory.getInstance().createEditor(doc, myProject);
    editor.getCaretModel().moveToOffset(offset);

    new CodeCompletionHandlerBase(CompletionType.BASIC, false, false, true).invokeCompletion(myProject, editor);

    List<LookupElement> elements =
      Optional.ofNullable(
        LookupManager
          .getActiveLookup(editor)
      ).map(Lookup::getItems).orElse(new LinkedList<>());

    List<String> actual = elements.stream()
      .map(LookupElement::getLookupString)
      .collect(Collectors.toList());

    WriteAction.runAndWait(() -> doc.setText(originalContent));

    assertSameElements(actual, expected);
    EditorFactory.getInstance().releaseEditor(editor);
  }
}
 
Example #7
Source File: BaseCodeCompletionAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public CodeCompletionHandlerBase createHandler(@Nonnull CompletionType completionType, boolean invokedExplicitly, boolean autopopup, boolean synchronous) {

  return new CodeCompletionHandlerBase(completionType, invokedExplicitly, autopopup, synchronous);
}
 
Example #8
Source File: CompleteMacro.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
protected void invokeCompletionHandler(Project project, Editor editor) {
  new CodeCompletionHandlerBase(CompletionType.BASIC, ApplicationManager.getApplication().isUnitTestMode(), false, true)
    .invokeCompletion(project, editor, 1);
}
 
Example #9
Source File: CompleteSmartMacro.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
protected void invokeCompletionHandler(Project project, Editor editor) {
  new CodeCompletionHandlerBase(CompletionType.SMART).invokeCompletion(project, editor, 1);
}
 
Example #10
Source File: ClassNameCompleteMacro.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
protected void invokeCompletionHandler(Project project, Editor editor) {
  new CodeCompletionHandlerBase(CompletionType.CLASS_NAME, false, false, true).invokeCompletion(project, editor);
}