Skip to content

Commit

Permalink
Spring properties postfix computed in completion item resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex committed Oct 9, 2023
1 parent 862fd9b commit 1dfba38
Show file tree
Hide file tree
Showing 32 changed files with 471 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static boolean hasDependencyStartingWith(IJavaProject jp, String dependen
log.error("", e);
}
return Optional.empty();
}).isEmpty();
}).isPresent();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -138,13 +139,22 @@ public enum Direction {
private class Insertion extends Edit {
private int offset;
private String text;
private Supplier<String> resolveInsert;
private boolean resolved;


public Insertion(boolean grabCursor, int offset, String insert) {
this(grabCursor, offset, insert, null);
}

public Insertion(boolean grabCursor, int offset, String insert, Supplier<String> resolveInsert) {
super(grabCursor);
this.offset = offset;
this.text = insert;
this.resolveInsert = resolveInsert;
this.resolved = resolveInsert == null;
}

@Override
void apply(DocumentState doc) throws BadLocationException {
doc.insert(grabCursor, offset, text);
Expand All @@ -164,6 +174,15 @@ public int getStart() {
public int getEnd() {
return offset;
}

public void resolve() {
text = resolveInsert.get();
resolved = true;
}

public boolean isResolved() {
return resolved;
}
}

private abstract class Edit {
Expand All @@ -176,6 +195,10 @@ protected Edit(boolean grabCursor) {
abstract void apply(DocumentState doc) throws BadLocationException;
@Override
public abstract String toString();
public boolean isResolved() {
return true;
}
public void resolve() {}
}

private class Deletion extends Edit {
Expand Down Expand Up @@ -365,7 +388,11 @@ public void insertSnippet(int offset, String snippet) {
public void insert(int offset, String insert) {
edits.add(new Insertion(grabCursor, offset, insert));
}


public void lazyInsert(int offset, String insert, Supplier<String> resolveText) {
edits.add(new Insertion(grabCursor, offset, insert, resolveText));
}

@Override
public IRegion getSelection() throws Exception {
DocumentState selectionState = new DocumentState(null);
Expand Down Expand Up @@ -487,6 +514,12 @@ public void transformFirstNonWhitespaceEdit(BiFunction<Integer, String, String>
Matcher matcher = NON_WS_CHAR.matcher(insert.text);
if (matcher.find()) {
insert.text = transformFun.apply(matcher.start(), insert.text);
if (!insert.isResolved()) {
Supplier<String> originalSupl = insert.resolveInsert;
insert.resolveInsert = () -> {
return transformFun.apply(matcher.start(), originalSupl.get());
};
}
}
}
}
Expand Down Expand Up @@ -524,10 +557,34 @@ public void dropPrefix(String prefix) {
if (ins.offset>=del.start && ins.offset <=del.end && replacedText.startsWith(prefix)) {
del.start+=prefix.length();
ins.text = ins.text.substring(prefix.length());
if (!ins.isResolved()) {
Supplier<String> originalSupl = ins.resolveInsert;
ins.resolveInsert = () -> {
return originalSupl.get().substring(prefix.length());
};
}
}
}
} catch (BadLocationException e) {
log.error("", e);
}
}

public boolean isResolved() {
for (Edit e : edits) {
if (!e.isResolved()) {
return false;
}
}
return true;
}

public void resolve() {
for (Edit edit : edits) {
if (!edit.isResolved()) {
edit.resolve();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package org.springframework.ide.vscode.commons.languageserver.completion;

import java.util.Optional;
import java.util.function.Supplier;

import org.eclipse.lsp4j.CompletionItemKind;
import org.springframework.ide.vscode.commons.util.Renderable;
Expand All @@ -25,13 +26,13 @@ public interface ICompletionProposal {
String getLabel();
CompletionItemKind getKind();
DocumentEdits getTextEdit();
default Optional<DocumentEdits> getAdditionalEdit() { return Optional.empty(); }
default Optional<Supplier<DocumentEdits>> getAdditionalEdit() { return Optional.empty(); }
default boolean isTriggeringNextCompletionRequest() { return false; }

String getDetail();
Renderable getDocumentation();
default String getFilterText() { return getLabel(); }

/**
* Transforms a proposal to make it standout less somehow.
* @param howmuch A 'weight' for the deemphasis. Allowing to deempasize some proposals more than others.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 Pivotal, Inc.
* Copyright (c) 2017, 2023 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
Expand All @@ -10,6 +10,9 @@
*******************************************************************************/
package org.springframework.ide.vscode.commons.languageserver.completion;

import java.util.Optional;
import java.util.function.Supplier;

import org.eclipse.lsp4j.CompletionItemKind;
import org.springframework.ide.vscode.commons.util.Renderable;

Expand Down Expand Up @@ -78,4 +81,10 @@ public double getBaseScore() {
public String getFilterText() {
return original.getFilterText();
}

@Override
public Optional<Supplier<DocumentEdits>> getAdditionalEdit() {
return original.getAdditionalEdit();
}

}
Loading

0 comments on commit 1dfba38

Please sign in to comment.