use of org.springframework.ide.vscode.commons.util.text.IRegion in project sts4 by spring-projects.
the class PropertiesHoverCalculator method getValueHover.
private Tuple2<Renderable, IRegion> getValueHover(Value value) {
DocumentRegion valueRegion = createRegion(doc, value).trimStart(SPACES).trimEnd(SPACES);
if (valueRegion.getStart() <= offset && offset < valueRegion.getEnd()) {
String valueString = valueRegion.toString();
String propertyName = value.getParent().getKey().decode();
Type type = getValueType(index, typeUtil, propertyName);
if (TypeUtil.isSequencable(type)) {
// It is useful to provide content assist for the values in the list when entering a list
type = TypeUtil.getDomainType(type);
}
if (TypeUtil.isClass(type)) {
// Special case. We want to provide hoverinfos more liberally than what's suggested for completions (i.e. even class names
// that are not suggested by the hints because they do not meet subtyping constraints should be hoverable and linkable!
StsValueHint hint = StsValueHint.className(valueString, typeUtil);
if (hint != null) {
return Tuples.of(createRenderable(hint), valueRegion.asRegion());
}
}
// Hack: pretend to invoke content-assist at the end of the value text. This should provide hints applicable to that value
// then show hoverinfo based on that. That way we can avoid duplication a lot of similar logic to compute hoverinfos and hyperlinks.
Collection<StsValueHint> hints = getValueHints(index, typeUtil, valueString, propertyName, EnumCaseMode.ALIASED);
if (hints != null) {
Optional<StsValueHint> hint = hints.stream().filter(h -> valueString.equals(h.getValue())).findFirst();
if (hint.isPresent()) {
return Tuples.of(createRenderable(hint.get()), valueRegion.asRegion());
}
}
}
return null;
}
use of org.springframework.ide.vscode.commons.util.text.IRegion in project sts4 by spring-projects.
the class YamlHoverInfoProvider method getHoverInfo.
@Override
public Tuple2<Renderable, IRegion> getHoverInfo(IDocument doc, int offset) throws Exception {
YamlFileAST ast = getAst(doc);
if (ast != null) {
IRegion region = getHoverRegion(ast, offset);
if (region != null) {
YamlDocument ymlDoc = new YamlDocument(doc, structureProvider);
YamlAssistContext assistContext = assistContextProvider.getGlobalAssistContext(ymlDoc);
if (assistContext != null) {
List<NodeRef<?>> astPath = ast.findPath(offset);
final YamlPath path = YamlPath.fromASTPath(astPath);
if (path != null) {
YamlPath assistPath = path;
if (assistPath.pointsAtKey()) {
// When a path points at a key we must tramsform it to a
// 'value-terminating path'
// to be able to reuse the 'getHoverInfo' method on
// YamlAssistContext (as navigation
// into 'key' is not defined for YamlAssistContext.
String key = path.getLastSegment().toPropString();
assistPath = path.dropLast().append(YamlPathSegment.valueAt(key));
}
assistContext = assistPath.traverse(assistContext);
if (assistContext != null) {
Renderable info = path.pointsAtValue() ? assistContext.getValueHoverInfo(ymlDoc, new DocumentRegion(doc, region)) : assistContext.getHoverInfo();
// Fix for: PT 134914895. If assist context cannot provide an info, then don't return a Tuple.
if (info != null) {
return Tuples.of(info, region);
}
}
}
}
}
}
return null;
}
use of org.springframework.ide.vscode.commons.util.text.IRegion in project sts4 by spring-projects.
the class YamlQuickfixes method getCursorPostionAfter.
private Position getCursorPostionAfter(TextDocument _doc, YamlPathEdits edits) {
try {
IRegion newSelection = edits.getSelection();
if (newSelection != null) {
// There is probably a more efficient way to compute the new cursor position. But its tricky...
// ... because we need to compute line/char coordinate, in terms of lines in the *new* document.
// So we have to take into account how newlines have been inserted or shifted around by the edits.
// Doing that without actually applying the edits is... difficult.
TextDocument doc = _doc.copy();
edits.apply(doc);
return doc.toPosition(newSelection.getOffset());
}
} catch (Exception e) {
Log.log(e);
}
return null;
}
use of org.springframework.ide.vscode.commons.util.text.IRegion in project sts4 by spring-projects.
the class YamlDocument method isCommented.
/**
* Determine whether given offset is inside a comment.
*/
public boolean isCommented(int offset) throws Exception {
// Yaml only has end of line comments marked with a '#'.
// So comments never span multiple lines of text and we only have scan back
// from offset upto the start of the current line.
IRegion lineInfo = doc.getLineInformationOfOffset(offset);
if (lineInfo != null) {
int startOfLine = lineInfo.getOffset();
while (offset >= startOfLine) {
char c = getChar(offset);
if (c == '#') {
return true;
}
offset--;
}
}
return false;
}
use of org.springframework.ide.vscode.commons.util.text.IRegion in project sts4 by spring-projects.
the class YamlDocument method getLineIndentation.
/**
* Returns the number of leading spaces in front of a line. If the line is effectively empty (only contains
* comments and/or spaces then this returns -1 (meaning undefined, as indentation level only really means
* something for lines which have 'real' content.
*/
public int getLineIndentation(int line) {
IRegion r;
try {
r = getLineInformation(line);
} catch (BadLocationException e) {
// not a line in the document so it has no indentation
return -1;
}
int len = r.getLength();
int startOfLine = r.getOffset();
int leadingSpaces = 0;
while (leadingSpaces < len) {
char c = getChar(startOfLine + leadingSpaces);
if (c == ' ') {
leadingSpaces++;
} else if (c == '#') {
return -1;
} else if (c != ' ') {
return leadingSpaces;
}
leadingSpaces++;
}
// Whole line scanned and nothing but spaces found
return -1;
}
Aggregations