eclipse-lsp4e / eclipse-lsp4e/lsp4e

Completion items are not sorted when extra text typed after completion invocation

Open
#842 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
85
Forks
69
Avg merge
8h 33m
Merged PRs (30d)
6

Description

The LSContentAssistProcessor sorts completions based on the rank when completions are first computed. If extra chars are typed after the initial CA invocation then the list of proposals is filtered (as invalid proposals are filtered out) but never sorted again based on the document filter text. The generic editor ContentAssistant is null thus nothing is sorted after the initial invocation. The result is irrelevant completion proposals ends up at the top of the list.
It'd be great if somehow we could set the sorter on the ContentAssistant... I couldn't find a way unfortunately and ended up using reflection just to test the hypothesis.
After writing the code below in lsp4e i was able to achieve the desired effect:

	private void installSorterIfAbsetnt(ITextViewer viewer) {
		if (viewer instanceof SourceViewer) {
			try {
				Field f = SourceViewer.class.getDeclaredField("fContentAssistant"); //$NON-NLS-1$
				f.setAccessible(true);
				Object o = f.get(viewer);
				if (o instanceof ContentAssistant) {
					ContentAssistant contentAssitant = (ContentAssistant) o;
					Field sorterField = ContentAssistant.class.getDeclaredField("fSorter"); //$NON-NLS-1$
					sorterField.setAccessible(true);
					if (sorterField.get(contentAssitant) == null) {
						contentAssitant.setSorter(new ICompletionProposalSorter() {

							@Override
							public int compare(ICompletionProposal p1, ICompletionProposal p2) {
								if (p1 instanceof LSCompletionProposal && p2 instanceof LSCompletionProposal) {
									return proposalComparator.compare((LSCompletionProposal) p1, (LSCompletionProposal) p2);
								}
								return 0;
							}
						});
					}
				}
			} catch (Exception e) {
				// ignore
			}
		}
	}

	@Override
	public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
		installSorterIfAbsetnt(viewer);

		IDocument document = viewer.getDocument();

                ....

I'd be more than happy to provide a patch if someone points in the right direction to implement this elegantly :-)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with LSContentAssistProcessor.computeCompletionProposals and trace how ContentAssistant filters proposals after extra document text is typed. Review the SourceViewer and ContentAssistant sorter behavior, then reproduce the issue with completion proposals; done means irrelevant proposals no longer remain at the top after filtering without relying on reflection.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.