From bf93fafabb3eec481cafadfa84f249421e7e9a8d Mon Sep 17 00:00:00 2001 From: nsingh Date: Wed, 12 Sep 2018 19:01:46 +0200 Subject: [PATCH] PT-160455522: Fix for empty string insertion for ad hoc props --- .../completion/ICompletionProposal.java | 5 ++++- .../VscodeCompletionEngineAdapter.java | 5 +---- .../java/value/ValueCompletionProcessor.java | 18 ++++++++++++++---- .../java/value/ValuePropertyKeyProposal.java | 18 ++++++++++++------ 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/headless-services/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/completion/ICompletionProposal.java b/headless-services/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/completion/ICompletionProposal.java index defec6285..23b759565 100644 --- a/headless-services/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/completion/ICompletionProposal.java +++ b/headless-services/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/completion/ICompletionProposal.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2016-2017 Pivotal, Inc. + * Copyright (c) 2016-2018 Pivotal, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -14,6 +14,7 @@ package org.springframework.ide.vscode.commons.languageserver.completion; import java.util.Optional; import org.eclipse.lsp4j.CompletionItemKind; +import org.eclipse.lsp4j.InsertTextFormat; import org.springframework.ide.vscode.commons.util.Renderable; /** @@ -37,4 +38,6 @@ public interface ICompletionProposal { */ default ICompletionProposal deemphasize(double howmuch) { return this; } + default InsertTextFormat getInsertTextFormat() { return InsertTextFormat.Snippet; } + } diff --git a/headless-services/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/completion/VscodeCompletionEngineAdapter.java b/headless-services/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/completion/VscodeCompletionEngineAdapter.java index a661cbc2e..8899bb095 100644 --- a/headless-services/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/completion/VscodeCompletionEngineAdapter.java +++ b/headless-services/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/completion/VscodeCompletionEngineAdapter.java @@ -20,7 +20,6 @@ import java.util.function.Consumer; import org.eclipse.lsp4j.CompletionItem; import org.eclipse.lsp4j.CompletionList; -import org.eclipse.lsp4j.InsertTextFormat; import org.eclipse.lsp4j.MarkupContent; import org.eclipse.lsp4j.MarkupKind; import org.eclipse.lsp4j.Position; @@ -29,11 +28,9 @@ import org.eclipse.lsp4j.TextEdit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits.TextReplace; -import org.springframework.ide.vscode.commons.languageserver.util.LspClient; import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer; import org.springframework.ide.vscode.commons.languageserver.util.SimpleTextDocumentService; import org.springframework.ide.vscode.commons.languageserver.util.SortKeys; -import org.springframework.ide.vscode.commons.languageserver.util.LspClient.Client; import org.springframework.ide.vscode.commons.util.BadLocationException; import org.springframework.ide.vscode.commons.util.Renderable; import org.springframework.ide.vscode.commons.util.StringUtil; @@ -197,7 +194,7 @@ public class VscodeCompletionEngineAdapter implements VscodeCompletionEngine { Optional mainEdit = adaptEdits(doc, completion.getTextEdit()); if (mainEdit.isPresent()) { item.setTextEdit(mainEdit.get()); - item.setInsertTextFormat(InsertTextFormat.Snippet); + item.setInsertTextFormat(completion.getInsertTextFormat()); } else { item.setInsertText(""); } diff --git a/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/value/ValueCompletionProcessor.java b/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/value/ValueCompletionProcessor.java index e9e0eeac6..954ce6d75 100644 --- a/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/value/ValueCompletionProcessor.java +++ b/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/value/ValueCompletionProcessor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2017 Pivotal, Inc. + * Copyright (c) 2017, 2018 Pivotal, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -24,6 +24,7 @@ import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.dom.MemberValuePair; import org.eclipse.jdt.core.dom.SimpleName; import org.eclipse.jdt.core.dom.StringLiteral; +import org.eclipse.lsp4j.InsertTextFormat; import org.springframework.ide.vscode.boot.java.handlers.CompletionProvider; import org.springframework.ide.vscode.boot.metadata.PropertyInfo; import org.springframework.ide.vscode.boot.metadata.SpringPropertyIndexProvider; @@ -63,7 +64,11 @@ public class ValueCompletionProcessor implements CompletionProvider { DocumentEdits edits = new DocumentEdits(doc); edits.replace(offset, offset, "\"${" + match.data.getId() + "}\""); - ValuePropertyKeyProposal proposal = new ValuePropertyKeyProposal(edits, match); + // PT-160455522: create a proposal with `PlainText` format type, because for vscode (but not Eclipse), if you send it as a snippet + // and it is "place holder" as such `"${debug}"`, vscode may treat it as a snippet place holder, and insert an empty string + // if it cannot resolve it. If sending this as plain text, then insertion happens correctly + ValuePropertyKeyProposal proposal = new ValuePropertyKeyProposal(edits, match, InsertTextFormat.PlainText); + result.add(proposal); } } @@ -114,7 +119,8 @@ public class ValueCompletionProcessor implements CompletionProvider { DocumentEdits edits = new DocumentEdits(doc); edits.replace(startOffset, endOffset, proposalPrefix + "${" + match.data.getId() + "}" + proposalPostfix); - ValuePropertyKeyProposal proposal = new ValuePropertyKeyProposal(edits, match); + ValuePropertyKeyProposal proposal = new ValuePropertyKeyProposal(edits, match, InsertTextFormat.PlainText); + completions.add(proposal); } } @@ -149,7 +155,11 @@ public class ValueCompletionProcessor implements CompletionProvider { DocumentEdits edits = new DocumentEdits(doc); edits.replace(startOffset, endOffset, preCompletion + match.data.getId() + postCompletion); - ValuePropertyKeyProposal proposal = new ValuePropertyKeyProposal(edits, match); + // PT 160455522: create a proposal with `PlainText` format type, because for vscode (but not Eclipse), if you send it as a snippet + // and the proposal value is "place holder" as such `"${debug}"`, vscode may treat it as a snippet place holder, and insert an empty string + // if it cannot resolve it. If sending this as plain text, then insertion happens correctly + ValuePropertyKeyProposal proposal = new ValuePropertyKeyProposal(edits, match, InsertTextFormat.PlainText); + completions.add(proposal); } } diff --git a/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/value/ValuePropertyKeyProposal.java b/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/value/ValuePropertyKeyProposal.java index c7dfb8272..4fc7245b7 100644 --- a/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/value/ValuePropertyKeyProposal.java +++ b/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/value/ValuePropertyKeyProposal.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2017 Pivotal, Inc. + * Copyright (c) 2017, 2018 Pivotal, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -11,14 +11,13 @@ package org.springframework.ide.vscode.boot.java.value; import org.eclipse.lsp4j.CompletionItemKind; +import org.eclipse.lsp4j.InsertTextFormat; import org.springframework.ide.vscode.boot.common.InformationTemplates; import org.springframework.ide.vscode.boot.metadata.PropertyInfo; import org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits; -import org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal; import org.springframework.ide.vscode.commons.languageserver.completion.ScoreableProposal; import org.springframework.ide.vscode.commons.util.FuzzyMap.Match; import org.springframework.ide.vscode.commons.util.Renderable; -import org.springframework.ide.vscode.commons.util.Renderables; /** * @author Martin Lippert @@ -30,17 +29,19 @@ public class ValuePropertyKeyProposal extends ScoreableProposal { private String detail; private Renderable documentation; private double score; + private InsertTextFormat textFormat; - private ValuePropertyKeyProposal(DocumentEdits edits, String label, String detail, double score, Renderable documentation) { + private ValuePropertyKeyProposal(DocumentEdits edits, String label, String detail, double score, Renderable documentation, InsertTextFormat textFormat) { this.edits = edits; this.label = label; this.detail = detail; this.documentation = documentation; this.score = score; + this.textFormat = textFormat; } - public ValuePropertyKeyProposal(DocumentEdits edits, Match match) { - this(edits, match.data.getId(), match.data.getType(), match.score, InformationTemplates.createCompletionDocumentation(match.data)); + public ValuePropertyKeyProposal(DocumentEdits edits, Match match, InsertTextFormat textFormat) { + this(edits, match.data.getId(), match.data.getType(), match.score, InformationTemplates.createCompletionDocumentation(match.data), textFormat); } @Override @@ -72,5 +73,10 @@ public class ValuePropertyKeyProposal extends ScoreableProposal { public double getBaseScore() { return score; } + + @Override + public InsertTextFormat getInsertTextFormat() { + return this.textFormat; + } }