use of nl.ramsolutions.sw.magik.analysis.Range in project magik-tools by StevenLooman.
the class HoverProvider method provideHover.
/**
* Provide a hover at the given position.
* @param magikFile Magik file.
* @param position Position in file.
* @return Hover at position.
*/
public Hover provideHover(final MagikTypedFile magikFile, final Position position) {
// Parse and reason magik.
final AstNode node = magikFile.getTopNode();
final AstNode hoveredTokenNode = AstQuery.nodeAt(node, Lsp4jConversion.positionFromLsp4j(position));
if (hoveredTokenNode == null) {
return null;
}
final AstNode hoveredNode = hoveredTokenNode.getParent();
LOGGER.debug("Hovering on: {}", hoveredNode.getTokenValue());
// See what we should provide a hover for.
final StringBuilder builder = new StringBuilder();
if (hoveredNode.is(MagikGrammar.IDENTIFIER) && hoveredNode.getParent().is(MagikGrammar.METHOD_DEFINITION)) {
// Method definition (exemplar or method name).
this.provideHoverMethodDefinition(magikFile, hoveredNode, builder);
} else if (hoveredNode.is(MagikGrammar.IDENTIFIER) && hoveredNode.getParent().is(MagikGrammar.METHOD_INVOCATION)) {
this.provideHoverMethodInvocation(magikFile, hoveredNode, builder);
} else if (hoveredNode.getParent().is(MagikGrammar.ATOM) || hoveredNode.getParent().is(MagikGrammar.SLOT)) {
final AstNode atomNode = hoveredNode.getFirstAncestor(MagikGrammar.ATOM);
this.provideHoverAtom(magikFile, atomNode, builder);
} else if (hoveredNode.getParent().is(MagikGrammar.PARAMETER)) {
final AstNode parameterNode = hoveredNode.getParent();
this.provideHoverAtom(magikFile, parameterNode, builder);
}
final MarkupContent contents = new MarkupContent(MarkupKind.MARKDOWN, builder.toString());
final Range range = new Range(hoveredTokenNode);
final org.eclipse.lsp4j.Range rangeLsp4j = Lsp4jConversion.rangeToLsp4j(range);
return new Hover(contents, rangeLsp4j);
}
use of nl.ramsolutions.sw.magik.analysis.Range in project magik-tools by StevenLooman.
the class MagikCheck method addIssue.
/**
* Add a new issue.
* @param startLine Start line of issue.
* @param startColumn Start column of issue.
* @param endLine End line of issue.
* @param endColumn End column of issue.
* @param message Message of issue.
*/
public void addIssue(final int startLine, final int startColumn, final int endLine, final int endColumn, final String message) {
final URI uri = this.getMagikFile().getUri();
final Position startPosition = new Position(startLine, startColumn);
final Position endPosition = new Position(endLine, endColumn);
final Range range = new Range(startPosition, endPosition);
final Location location = new Location(uri, range);
this.addIssue(location, message);
}
use of nl.ramsolutions.sw.magik.analysis.Range in project magik-tools by StevenLooman.
the class LineLengthCheck method walkPreMagik.
@Override
protected void walkPreMagik(final AstNode node) {
final MagikFile magikFile = this.getMagikFile();
String[] lines = magikFile.getSourceLines();
if (lines == null) {
lines = new String[] {};
}
int lineNo = 1;
for (final String line : lines) {
final AtomicInteger columnNo = new AtomicInteger(0);
final AtomicInteger issueColumnNo = new AtomicInteger(0);
line.chars().forEachOrdered(chr -> {
final int cur;
if (chr == '\t') {
cur = columnNo.addAndGet(TAB_WIDTH);
} else {
cur = columnNo.incrementAndGet();
}
if (cur <= this.maxLineLength + 1) {
issueColumnNo.incrementAndGet();
}
});
if (columnNo.get() > this.maxLineLength) {
final URI uri = this.getMagikFile().getUri();
final Position startPosition = new Position(lineNo, issueColumnNo.get() - 1);
final Position endPosition = new Position(lineNo, line.length());
final Range range = new Range(startPosition, endPosition);
final Location location = new Location(uri, range);
final String message = String.format(MESSAGE, line.length(), this.maxLineLength);
this.addIssue(location, message);
}
++lineNo;
}
}
use of nl.ramsolutions.sw.magik.analysis.Range in project magik-tools by StevenLooman.
the class RenameProvider method providePrepareRename.
/**
* Provide prepare rename.
* @param magikFile Magik file.
* @param position Position in magik source.
* @return Prepare rename or null if no rename possible.
*/
public Either<org.eclipse.lsp4j.Range, PrepareRenameResult> providePrepareRename(final MagikTypedFile magikFile, final Position position) {
// Parse magik.
final AstNode topNode = magikFile.getTopNode();
// Should always be on an identifier.
final AstNode node = AstQuery.nodeAt(topNode, Lsp4jConversion.positionFromLsp4j(position), MagikGrammar.IDENTIFIER);
if (node == null) {
return null;
}
// Set up scope.
final ScopeEntry scopeEntry = this.findScopeEntry(magikFile, node);
if (scopeEntry == null || scopeEntry.isType(ScopeEntry.Type.GLOBAL) || scopeEntry.isType(ScopeEntry.Type.DYNAMIC) || scopeEntry.isType(ScopeEntry.Type.IMPORT)) {
return null;
}
final Range range = new Range(node);
final org.eclipse.lsp4j.Range rangeLsp4j = Lsp4jConversion.rangeToLsp4j(range);
final String identifier = node.getTokenOriginalValue();
final PrepareRenameResult result = new PrepareRenameResult(rangeLsp4j, identifier);
return Either.forRight(result);
}
use of nl.ramsolutions.sw.magik.analysis.Range in project magik-tools by StevenLooman.
the class Lsp4jConversion method locationToLsp4j.
/**
* Convert a Location to LSP4J.
* @param location Location to convert.
* @return Location in LSP4J.
*/
public static org.eclipse.lsp4j.Location locationToLsp4j(final Location location) {
final String uri = location.getUri().toString();
final Range range = location.getRange();
final org.eclipse.lsp4j.Range rangeLsp4j = Lsp4jConversion.rangeToLsp4j(range);
return new org.eclipse.lsp4j.Location(uri, rangeLsp4j);
}
Aggregations