Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import de.peeeq.wurstscript.ast.ExprRealVal;
import de.peeeq.wurstscript.ast.ExprStringVal;
import de.peeeq.wurstscript.ast.FuncRef;
import de.peeeq.wurstscript.ast.ModuleInstanciation;
import de.peeeq.wurstscript.ast.NameRef;
import de.peeeq.wurstscript.attributes.names.FuncLink;
import org.eclipse.lsp4j.InlayHint;
Expand Down Expand Up @@ -68,6 +69,12 @@ public List<InlayHint> execute(ModelManager modelManager) {
todo.push(cu);
while (!todo.isEmpty()) {
Element e = todo.pop();
// Module instantiations are compiler-generated copies of module bodies. Their children
// retain the source positions of the module definition, which are not positions in the
// document containing the `use` statement.
if (e instanceof ModuleInstanciation) {
continue;
}
collectHints(hints, e);
for (int i = e.size() - 1; i >= 0; i--) {
todo.push(e.get(i));
Expand Down Expand Up @@ -125,6 +132,9 @@ private void addParameterHints(List<InlayHint> hints, Arguments args, List<Strin
Map<String, Integer> typeFrequencies = typeFrequencies(paramTypes);
for (int i = 0; i < count; i++) {
Expr arg = args.get(i);
if (!isConcreteSourceInRequestedFile(arg)) {
continue;
}
String paramName = paramNames.get(i);
if (paramName == null || paramName.isEmpty()) {
continue;
Expand All @@ -143,6 +153,11 @@ private void addParameterHints(List<InlayHint> hints, Arguments args, List<Strin
}
}

private boolean isConcreteSourceInRequestedFile(Expr arg) {
return !arg.attrSource().isArtificial()
&& WFile.create(arg.attrSource().getFile()).equals(filename);
}

private int hintScore(Expr arg, String paramName, String paramType, Map<String, Integer> typeFrequencies) {
int score = 0;
String argName = argumentName(arg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -549,6 +550,37 @@ public void inlayHintForMemberAccessStartsAtReceiver() throws IOException {
assertEquals(idHint.getPosition().getCharacter(), expectedStart);
}

@Test
public void inlayHintsIgnoreCopiedModuleBodyFromAnotherFile() throws IOException {
CompletionTestData data = input(
"package test",
"import HintModule",
"class Mover",
" use HintModule",
"",
"",
"endpackage"
);
String module = String.join("\n",
"package HintModule",
"public module HintModule",
" function target(int amount)",
" skip",
" function callTarget()",
" target(1)",
"endpackage",
"");
TestContext ctx = createContext(data, data.buffer, Map.of("HintModule.wurst", module));

InlayHintParams params = new InlayHintParams(
new TextDocumentIdentifier(ctx.uri),
new Range(new Position(0, 0), new Position(100, 0))
);
List<InlayHint> hints = new InlayHintsRequest(params, ctx.bufferManager).execute(ctx.modelManager);

assertTrue(hints.isEmpty(), "Hints from a copied module body must not appear in the using file: " + hints);
}

@Test
public void inlayHintsStayStableWhileTemporarilyUnparsable() throws IOException {
CompletionTestData valid = input(
Expand Down Expand Up @@ -870,6 +902,11 @@ private int compare(Position a, Position b) {
}

private TestContext createContext(CompletionTestData data, String diskContent) throws IOException {
return createContext(data, diskContent, Collections.emptyMap());
}

private TestContext createContext(CompletionTestData data, String diskContent,
Map<String, String> additionalFiles) throws IOException {
File projectFolder = new File("./temp/lspNative/" + System.nanoTime());
File wurstFolder = new File(projectFolder, "wurst");
Files.createDirectories(wurstFolder.toPath());
Expand All @@ -878,6 +915,9 @@ private TestContext createContext(CompletionTestData data, String diskContent) t
File wurstFile = new File(wurstFolder, "Wurst.wurst");
Files.writeString(testFile.toPath(), diskContent);
Files.writeString(wurstFile.toPath(), "package Wurst\n");
for (Map.Entry<String, String> additionalFile : additionalFiles.entrySet()) {
Files.writeString(new File(wurstFolder, additionalFile.getKey()).toPath(), additionalFile.getValue());
}

BufferManager bufferManager = new BufferManager();
ModelManagerImpl modelManager = new ModelManagerImpl(projectFolder.getAbsoluteFile(), bufferManager);
Expand Down
Loading