Skip to content

Commit

Permalink
CHE-5154: use new implementation of the Language Server Protocol for …
Browse files Browse the repository at this point in the history
…python (#5267)
  • Loading branch information
Valeriy Svydenko authored Jun 7, 2017
1 parent 9b6546c commit 6a4e383
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ fi

curl -s ${AGENT_BINARIES_URI} | tar xzf - -C ${LS_DIR}

cd ${LS_DIR} && ${SUDO} pip3 install -r ${LS_DIR}/requirements.txt
cd ${LS_DIR} && ${SUDO} pip3 install --process-dependency-links .

touch ${LS_LAUNCHER}
chmod +x ${LS_LAUNCHER}
echo "python3.5 ${LS_DIR}/python-langserver.py --fs=local --mode=stdio" > ${LS_LAUNCHER}
echo "pyls" > ${LS_LAUNCHER}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.ServerCapabilities;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.TextEdit;

import java.util.List;

Expand All @@ -47,6 +48,7 @@
*/
public class CompletionItemBasedCompletionProposal implements CompletionProposal {

private final String currentWord;
private final TextDocumentServiceClient documentServiceClient;
private final TextDocumentIdentifier documentId;
private final LanguageServerResources resources;
Expand All @@ -58,6 +60,7 @@ public class CompletionItemBasedCompletionProposal implements CompletionProposal
private boolean resolved;

CompletionItemBasedCompletionProposal(ExtendedCompletionItem completionItem,
String currentWord,
TextDocumentServiceClient documentServiceClient,
TextDocumentIdentifier documentId,
LanguageServerResources resources,
Expand All @@ -66,6 +69,7 @@ public class CompletionItemBasedCompletionProposal implements CompletionProposal
List<Match> highlights,
int offset) {
this.completionItem = completionItem;
this.currentWord = currentWord;
this.documentServiceClient = documentServiceClient;
this.documentId = documentId;
this.resources = resources;
Expand Down Expand Up @@ -167,7 +171,13 @@ public Icon getIcon() {

@Override
public void getCompletion(final CompletionCallback callback) {
callback.onCompletion(new CompletionImpl(completionItem, offset));
if (canResolve()) {
resolve().then(completionItem -> {
callback.onCompletion(new CompletionImpl(completionItem, currentWord, offset));
});
} else {
callback.onCompletion(new CompletionImpl(completionItem, currentWord, offset));
}
}

private boolean canResolve() {
Expand All @@ -185,10 +195,12 @@ private Promise<ExtendedCompletionItem> resolve() {
private static class CompletionImpl implements Completion {

private CompletionItem completionItem;
private String currentWord;
private int offset;

public CompletionImpl(CompletionItem completionItem, int offset) {
public CompletionImpl(CompletionItem completionItem, String currentWord, int offset) {
this.completionItem = completionItem;
this.currentWord = currentWord;
this.offset = offset;
}

Expand All @@ -202,17 +214,25 @@ public void apply(Document document) {
new TextPosition(range.getEnd().getLine(), range.getEnd().getCharacter()));
document.replace(startOffset, endOffset - startOffset, completionItem.getTextEdit().getNewText());
} else {
String insertText = completionItem.getInsertText() == null ? completionItem.getLabel() : completionItem.getInsertText();
document.replace(document.getCursorOffset() - offset, offset, insertText);
int currentWordLength = currentWord.length();
int cursorOffset = document.getCursorOffset();
if (completionItem.getInsertText() == null) {
document.replace(cursorOffset - currentWordLength, currentWordLength, completionItem.getLabel());
} else {
document.replace(cursorOffset - offset, offset, completionItem.getInsertText());
}
}
}

@Override
public LinearRange getSelection(Document document) {
Range range = completionItem.getTextEdit().getRange();
int startOffset = document
.getIndexFromPosition(new TextPosition(range.getStart().getLine(), range.getStart().getCharacter()))
+ completionItem.getTextEdit().getNewText().length();
final TextEdit textEdit = completionItem.getTextEdit();
if (textEdit == null) {
return LinearRange.createWithStart(document.getCursorOffset()).andLength(0);
}
Range range = textEdit.getRange();
TextPosition textPosition = new TextPosition(range.getStart().getLine(), range.getStart().getCharacter());
int startOffset = document.getIndexFromPosition(textPosition) + textEdit.getNewText().length();
return LinearRange.createWithStart(startOffset).andLength(0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ private void computeProposals(String currentWord, int offset, CodeAssistCallback
List<Match> highlights = filter(currentWord, item);
if (highlights != null) {
proposals.add(new CompletionItemBasedCompletionProposal(item,
currentWord,
documentServiceClient,
latestCompletionResult.getDocumentId(),
resources,
Expand Down

0 comments on commit 6a4e383

Please sign in to comment.