Java源码示例:com.intellij.codeInsight.completion.CodeCompletionHandlerBase

示例1
/**
 * 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);
        }
    }
}
 
示例2
@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);
}
 
示例3
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);
}
 
示例4
@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);
}
 
示例5
@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();
}
 
示例6
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);
  }
}
 
示例7
@Nonnull
public CodeCompletionHandlerBase createHandler(@Nonnull CompletionType completionType, boolean invokedExplicitly, boolean autopopup, boolean synchronous) {

  return new CodeCompletionHandlerBase(completionType, invokedExplicitly, autopopup, synchronous);
}
 
示例8
@Override
protected void invokeCompletionHandler(Project project, Editor editor) {
  new CodeCompletionHandlerBase(CompletionType.BASIC, ApplicationManager.getApplication().isUnitTestMode(), false, true)
    .invokeCompletion(project, editor, 1);
}
 
示例9
@Override
protected void invokeCompletionHandler(Project project, Editor editor) {
  new CodeCompletionHandlerBase(CompletionType.SMART).invokeCompletion(project, editor, 1);
}
 
示例10
@Override
protected void invokeCompletionHandler(Project project, Editor editor) {
  new CodeCompletionHandlerBase(CompletionType.CLASS_NAME, false, false, true).invokeCompletion(project, editor);
}