Java Code Examples for org.eclipse.lsp4j.Range#getStart()

The following examples show how to use org.eclipse.lsp4j.Range#getStart() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RenameProvider.java    From groovy-language-server with Apache License 2.0 6 votes vote down vote up
private TextEdit createTextEditToRenameMethodNode(MethodNode methodNode, String newName, String text, Range range) {
	// the AST doesn't give us access to the name location, so we
	// need to find it manually
	Pattern methodPattern = Pattern.compile("\\b" + methodNode.getName() + "\\b(?=\\s*\\()");
	Matcher methodMatcher = methodPattern.matcher(text);
	if (!methodMatcher.find()) {
		// couldn't find the name!
		return null;
	}

	Position start = range.getStart();
	Position end = range.getEnd();
	end.setCharacter(start.getCharacter() + methodMatcher.end());
	start.setCharacter(start.getCharacter() + methodMatcher.start());

	TextEdit textEdit = new TextEdit();
	textEdit.setRange(range);
	textEdit.setNewText(newName);
	return textEdit;
}
 
Example 2
Source File: RenameProvider.java    From groovy-language-server with Apache License 2.0 6 votes vote down vote up
private TextEdit createTextEditToRenamePropertyNode(PropertyNode propNode, String newName, String text,
		Range range) {
	// the AST doesn't give us access to the name location, so we
	// need to find it manually
	Pattern propPattern = Pattern.compile("\\b" + propNode.getName() + "\\b");
	Matcher propMatcher = propPattern.matcher(text);
	if (!propMatcher.find()) {
		// couldn't find the name!
		return null;
	}

	Position start = range.getStart();
	Position end = range.getEnd();
	end.setCharacter(start.getCharacter() + propMatcher.end());
	start.setCharacter(start.getCharacter() + propMatcher.start());

	TextEdit textEdit = new TextEdit();
	textEdit.setRange(range);
	textEdit.setNewText(newName);
	return textEdit;
}
 
Example 3
Source File: TextEditUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static String apply(Document doc, Collection<? extends TextEdit> edits) throws BadLocationException {
	Assert.isNotNull(doc);
	Assert.isNotNull(edits);
	List<TextEdit> sortedEdits = new ArrayList<>(edits);
	sortByLastEdit(sortedEdits);
	String text = doc.get();
	for (int i = sortedEdits.size() - 1; i >= 0; i--) {
		TextEdit te = sortedEdits.get(i);
		Range r = te.getRange();
		if (r != null && r.getStart() != null && r.getEnd() != null) {
			int start = getOffset(doc, r.getStart());
			int end = getOffset(doc, r.getEnd());
			text = text.substring(0, start)
					+ te.getNewText()
					+ text.substring(end, text.length());
		}
	}
	return text;
}
 
Example 4
Source File: RenameProvider.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
private TextEdit createTextEditToRenameClassNode(ClassNode classNode, String newName, String text, Range range) {
	// the AST doesn't give us access to the name location, so we
	// need to find it manually
	String className = classNode.getNameWithoutPackage();
	int dollarIndex = className.indexOf('$');
	if (dollarIndex != 01) {
		// it's an inner class, so remove the outer name prefix
		className = className.substring(dollarIndex + 1);
	}

	Pattern classPattern = Pattern.compile("(class\\s+)" + className + "\\b");
	Matcher classMatcher = classPattern.matcher(text);
	if (!classMatcher.find()) {
		// couldn't find the name!
		return null;
	}
	String prefix = classMatcher.group(1);

	Position start = range.getStart();
	Position end = range.getEnd();
	end.setCharacter(start.getCharacter() + classMatcher.end());
	start.setCharacter(start.getCharacter() + prefix.length() + classMatcher.start());

	TextEdit textEdit = new TextEdit();
	textEdit.setRange(range);
	textEdit.setNewText(newName);
	return textEdit;
}
 
Example 5
Source File: XMLRename.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
private List<TextEdit> getTagNameRenameTextEdits(DOMDocument xmlDocument, DOMElement element, Position position, String newText) {
	
	Range startTagRange = getTagNameRange(TokenType.StartTag, element.getStart(), xmlDocument);
	Range endTagRange = element.hasEndTag() ? getTagNameRange(TokenType.EndTag, element.getEndTagOpenOffset(), xmlDocument)
		: null;
	
	//Check if xsd namespace rename
	String fullNodeName = element.getNodeName();
	int indexOfColon = fullNodeName.indexOf(":");
	if(indexOfColon > 0) {
		Position startTagStartPosition = startTagRange.getStart();
		Position startTagPrefixPosition = new Position(startTagStartPosition.getLine(), startTagStartPosition.getCharacter() + indexOfColon);

		Position endTagStartPosition = endTagRange.getStart();
		Position endTagPrefixPosition = new Position(endTagStartPosition.getLine(), endTagStartPosition.getCharacter() + indexOfColon);

		Range startTagPrefixRange = new Range(startTagStartPosition, startTagPrefixPosition);
		Range endTagPrefixRange = new Range(endTagStartPosition, endTagPrefixPosition);

		if (doesTagCoverPosition(startTagPrefixRange, endTagPrefixRange, position)) {// Element prefix rename
			String prefix = element.getPrefix();
			return renameElementNamespace(xmlDocument, element, prefix.length(), newText);
		} else { //suffix rename without wiping namespace
			String suffixName = element.getLocalName();
			int suffixLength = suffixName.length();
			Position startTagEndPosition = startTagRange.getEnd();
			Position suffixStartPositionStart = new Position(startTagEndPosition.getLine(), startTagEndPosition.getCharacter() - suffixLength);

			Position endTagEndPosition = endTagRange.getEnd();
			Position suffixEndPositionStart = new Position(endTagEndPosition.getLine(), endTagEndPosition.getCharacter() - suffixLength);

			Range suffixRangeStart = new Range(suffixStartPositionStart, startTagEndPosition);
			Range suffixRangeEnd = new Range(suffixEndPositionStart, endTagEndPosition);

			return getRenameList(suffixRangeStart, suffixRangeEnd, newText);
		}
	}
	//Regular tag name rename
	return getRenameList(startTagRange, endTagRange, newText);
}
 
Example 6
Source File: XMLAssert.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
private static void assertCompletion(CompletionList completions, CompletionItem expected, TextDocument document,
		int offset, Integer expectedCount) {
	List<CompletionItem> matches = completions.getItems().stream().filter(completion -> {
		return expected.getLabel().equals(completion.getLabel());
	}).collect(Collectors.toList());

	if (expectedCount != null) {
		assertTrue(matches.size() >= 1, () -> {
			return expected.getLabel() + " should only exist once: Actual: "
					+ completions.getItems().stream().map(c -> c.getLabel()).collect(Collectors.joining(","));
		});
	} else {
		assertEquals(1, matches.size(), () -> {
			return expected.getLabel() + " should only exist once: Actual: "
					+ completions.getItems().stream().map(c -> c.getLabel()).collect(Collectors.joining(","));
		});
	}

	CompletionItem match = getCompletionMatch(matches, expected);
	if (expected.getTextEdit() != null && match.getTextEdit() != null) {
		if (expected.getTextEdit().getNewText() != null) {
			assertEquals(expected.getTextEdit().getNewText(), match.getTextEdit().getNewText());
		}
		Range r = expected.getTextEdit().getRange();
		if (r != null && r.getStart() != null && r.getEnd() != null) {
			assertEquals(expected.getTextEdit().getRange(), match.getTextEdit().getRange());
		}
	}
	if (expected.getFilterText() != null && match.getFilterText() != null) {
		assertEquals(expected.getFilterText(), match.getFilterText());
	}

	if (expected.getDocumentation() != null) {
		assertEquals(expected.getDocumentation(), match.getDocumentation());
	}

}
 
Example 7
Source File: StringLSP4J.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/** @return string for given element */
public String toString(Range range) {
	if (range == null) {
		return "";
	}
	Position start = range.getStart();
	Position end = range.getEnd();
	String stringPosStr = start.getLine() + ":" + start.getCharacter();
	String endPosStr = end.getLine() + ":" + end.getCharacter();
	return "[" + stringPosStr + " - " + endPosStr + "]";
}
 
Example 8
Source File: DiagnosticIssueConverter.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/** Convert the given diagnostic to an issue. */
public Issue toIssue(URI uri, Diagnostic diagnostic, Optional<Document> document) {
	IssueImpl issue = new Issue.IssueImpl();

	issue.setSeverity(toSeverity(diagnostic.getSeverity()));

	Range range = diagnostic.getRange();
	Position sPos = range.getStart();
	Position ePos = range.getEnd();
	int offSetStart = 0;
	int offSetEnd = 0;
	if (document.isPresent()) {
		offSetStart = document.get().getOffSet(new Position(sPos.getLine() + 1, sPos.getCharacter() + 1));
		offSetEnd = document.get().getOffSet(new Position(ePos.getLine() + 1, ePos.getCharacter() + 1));
	}

	issue.setLineNumber(sPos.getLine() + 1);
	issue.setColumn(sPos.getCharacter() + 1);
	issue.setOffset(offSetStart);
	issue.setLength(offSetEnd - offSetStart);

	issue.setUriToProblem(uri);
	issue.setCode(diagnostic.getCode());
	issue.setType(CheckType.FAST);
	issue.setMessage(diagnostic.getMessage());

	return issue;
}
 
Example 9
Source File: FileTracker.java    From vscode-as3mxml with Apache License 2.0 5 votes vote down vote up
private String patch(String sourceText, TextDocumentContentChangeEvent change)
{
    Range range = change.getRange();
    Position start = range.getStart();
    StringReader reader = new StringReader(sourceText);
    int offset = LanguageServerCompilerUtils.getOffsetFromPosition(reader, start);
    StringBuilder builder = new StringBuilder();
    builder.append(sourceText.substring(0, offset));
    builder.append(change.getText());
    builder.append(sourceText.substring(offset + change.getRangeLength()));
    return builder.toString();
}
 
Example 10
Source File: N4JSQuickfixProvider.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
private EObject getEObject(Document doc, XtextResource resource, Range range) {
	Position start = range.getStart();
	int startOffset = doc.getOffSet(start);
	return eObjectAtOffsetHelper.resolveContainedElementAt(resource, startOffset);
}
 
Example 11
Source File: JSONCodeActionService.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
private EObject getEObject(Document doc, XtextResource resource, Range range) {
	Position start = range.getStart();
	int startOffset = doc.getOffSet(start);
	return eObjectAtOffsetHelper.resolveContainedElementAt(resource, startOffset);
}