intellivim icon indicating copy to clipboard operation
intellivim copied to clipboard

Investigate simpler code completion

Open dhleong opened this issue 9 years ago • 0 comments

Discovered that CodeInsightTestFixtureImpl has a much simpler way of doing code completion using the Editor. Since we're using real editors now, it might work (though it might also do UI stuff we don't want):

  @Override
  public LookupElement[] complete(@NotNull 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();
  }

  @Nullable
  protected Editor getCompletionEditor() {
    return InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(myEditor, getFile());
  }

  @Override
  @Nullable
  public LookupElement[] getLookupElements() {
    LookupImpl lookup = getLookup();
    if (lookup == null) {
      return myEmptyLookup ? LookupElement.EMPTY_ARRAY : null;
    }
    else {
      final List<LookupElement> list = lookup.getItems();
      return list.toArray(new LookupElement[list.size()]);
    }
  }

  @Override
  public LookupImpl getLookup() {
    return (LookupImpl)LookupManager.getActiveLookup(myEditor);
  }

Would be great if it works, because our current method uses reflection and deprecated stuff.... Not ideal

dhleong avatar Apr 11 '15 14:04 dhleong