-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework variable hover to have a more compact layout (#435)
closes #422
- Loading branch information
Showing
3 changed files
with
183 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
libs/natls/src/main/java/org/amshove/natls/hover/VariableContextHover.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package org.amshove.natls.hover; | ||
|
||
import org.amshove.natls.markupcontent.IMarkupContentBuilder; | ||
import org.amshove.natparse.NodeUtil; | ||
import org.amshove.natparse.natural.IHasDefineData; | ||
import org.amshove.natparse.natural.INaturalModule; | ||
import org.amshove.natparse.natural.IUsingNode; | ||
import org.amshove.natparse.natural.IVariableNode; | ||
|
||
class VariableContextHover | ||
{ | ||
private final IVariableNode variable; | ||
private INaturalModule declaringModule; | ||
private IUsingNode usingNode; | ||
private String usingComment; | ||
|
||
private VariableContextHover(IVariableNode variable) | ||
{ | ||
this.variable = variable; | ||
} | ||
|
||
public void addVariableContext(IMarkupContentBuilder builder) | ||
{ | ||
var variableContext = new StringBuilder(); | ||
|
||
appendUsingComment(variableContext); | ||
if (variable.level() > 1) | ||
{ | ||
var group = NodeUtil.findLevelOneParentOf(variable); | ||
appendVariableComment(group, variableContext, true); | ||
} | ||
|
||
appendVariableComment(variable, variableContext, !variableContext.isEmpty()); | ||
|
||
if (!variableContext.isEmpty()) | ||
{ | ||
builder.appendSection("context", nestedBuilder -> nestedBuilder.appendCode(variableContext.toString())); | ||
} | ||
} | ||
|
||
private void appendUsingComment(StringBuilder chain) | ||
{ | ||
if (usingNode == null) | ||
{ | ||
return; | ||
} | ||
|
||
var usingSource = "%s USING %s".formatted(usingNode.scope().toSyntaxKind().name(), usingNode.target().symbolName()); | ||
appendComment("%s %s".formatted(usingSource, usingComment), chain); | ||
} | ||
|
||
private void appendVariableComment(IVariableNode variable, StringBuilder chain, boolean alwaysInclude) | ||
{ | ||
var source = "%d %s".formatted(variable.level(), variable.name()); | ||
var comment = declaringModule.extractLineComment(variable.position().line()); | ||
if (alwaysInclude || !comment.isEmpty()) | ||
{ | ||
appendComment("%s %s".formatted(source, comment), chain); | ||
} | ||
} | ||
|
||
private void appendComment(String comment, StringBuilder chain) | ||
{ | ||
if (comment != null && !comment.isEmpty()) | ||
{ | ||
if (!chain.isEmpty()) | ||
{ | ||
chain.append(System.lineSeparator()); | ||
} | ||
chain.append(comment); | ||
} | ||
} | ||
|
||
static VariableContextHover create(HoverContext hoverContext, IVariableNode variable) | ||
{ | ||
var variableContext = new VariableContextHover(variable); | ||
if (variable.position().filePath().equals(hoverContext.file().getPath())) | ||
{ | ||
variableContext.declaringModule = hoverContext.file().module(); | ||
} | ||
else | ||
{ | ||
var using = findUsingImportingVariable(variable, ((IHasDefineData) hoverContext.file().module())); | ||
if (using != null) | ||
{ | ||
variableContext.usingNode = using; | ||
variableContext.usingComment = hoverContext.file().module().extractLineComment(using.position().line()); | ||
variableContext.declaringModule = hoverContext.file().findNaturalModule(using.target().symbolName()); | ||
} | ||
} | ||
|
||
return variableContext; | ||
} | ||
|
||
private static IUsingNode findUsingImportingVariable(IVariableNode variable, IHasDefineData module) | ||
{ | ||
for (var using : module.defineData().usings()) | ||
{ | ||
if (using.defineData() != null && using.defineData().findVariable(variable.qualifiedName()) != null) | ||
{ | ||
return using; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters