Java Code Examples for org.eclipse.lsp4j.Diagnostic#getRange()

The following examples show how to use org.eclipse.lsp4j.Diagnostic#getRange() . 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: CodeActionProvider.java    From vscode-as3mxml with Apache License 2.0 6 votes vote down vote up
private void createCodeActionsForUnusedImport(Path path, Diagnostic diagnostic, WorkspaceFolderData folderData, List<Either<Command, CodeAction>> codeActions)
{
    String fileText = fileTracker.getText(path);
    if(fileText == null)
    {
        return;
    }

    Range range = diagnostic.getRange();
    WorkspaceEdit edit = CodeActionsUtils.createWorkspaceEditForRemoveUnusedImport(fileText, path.toUri().toString(), diagnostic.getRange());
    if (edit == null)
    {
        return;
    }

    int startOffset = LanguageServerCompilerUtils.getOffsetFromPosition(new StringReader(fileText), range.getStart());
    int endOffset = LanguageServerCompilerUtils.getOffsetFromPosition(new StringReader(fileText), range.getEnd());

    String importText = fileText.substring(startOffset, endOffset);
    CodeAction codeAction = new CodeAction();
    codeAction.setTitle("Remove " + importText);
    codeAction.setEdit(edit);
    codeAction.setKind(CodeActionKind.QuickFix);
    codeAction.setDiagnostics(Collections.singletonList(diagnostic));
    codeActions.add(Either.forRight(codeAction));
}
 
Example 2
Source File: WorkspaceDiagnosticsHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testMavenMarkers() throws Exception {
	String msg1 = "Some dependency is missing";
	IMarker m1 = createMavenMarker(IMarker.SEVERITY_ERROR, msg1, 2, 95, 100);

	IDocument d = mock(IDocument.class);
	when(d.getLineOffset(1)).thenReturn(90);

	List<Diagnostic> diags = WorkspaceDiagnosticsHandler.toDiagnosticsArray(d, new IMarker[] { m1, null }, true);
	assertEquals(1, diags.size());

	Range r;
	Diagnostic d1 = diags.get(0);
	assertEquals(msg1, d1.getMessage());
	assertEquals(DiagnosticSeverity.Error, d1.getSeverity());
	r = d1.getRange();
	assertEquals(1, r.getStart().getLine());
	assertEquals(95, r.getStart().getCharacter());
	assertEquals(1, r.getEnd().getLine());
	assertEquals(100, r.getEnd().getCharacter());
}
 
Example 3
Source File: ValidatorTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testMultilineDiagnostic_02() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("type Multiline_Demo2 {");
  _builder.newLine();
  _builder.append("string sample}");
  _builder.newLine();
  this.writeFile("MyType1.testlang", Strings.toUnixLineSeparator(_builder));
  this.initialize();
  final List<Diagnostic> problems = IterableExtensions.<Map.Entry<String, List<Diagnostic>>>head(this.getDiagnostics().entrySet()).getValue();
  String _join = IterableExtensions.join(problems, "\n");
  String _plus = ("problems found:\n" + _join);
  Assert.assertEquals(_plus, 1, problems.size());
  final Diagnostic problem = IterableExtensions.<Diagnostic>head(problems);
  this.assertEquals("Test Validation to mark the whole type", problem.getMessage());
  Assert.assertEquals(TestLanguageValidator.MULTILINE_PROBLEM, problem.getCode().get());
  final Range range = problem.getRange();
  Assert.assertEquals(0, range.getStart().getLine());
  Assert.assertEquals(0, range.getStart().getCharacter());
  Assert.assertEquals(1, range.getEnd().getLine());
  Assert.assertEquals(14, range.getEnd().getCharacter());
}
 
Example 4
Source File: ValidatorTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testDiagnosticAtEndOfLineIncludingNewline() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("type");
  _builder.newLine();
  this.writeFile("MyType1.testlang", Strings.toUnixLineSeparator(_builder));
  this.initialize();
  final List<Diagnostic> problems = IterableExtensions.<Map.Entry<String, List<Diagnostic>>>head(this.getDiagnostics().entrySet()).getValue();
  String _join = IterableExtensions.join(problems, "\n");
  String _plus = ("problems found:\n" + _join);
  Assert.assertEquals(_plus, 1, problems.size());
  final Diagnostic problem = IterableExtensions.<Diagnostic>head(problems);
  this.assertEquals("mismatched input \'<EOF>\' expecting RULE_ID", problem.getMessage());
  Assert.assertEquals(org.eclipse.xtext.diagnostics.Diagnostic.SYNTAX_DIAGNOSTIC, problem.getCode().get());
  final Range range = problem.getRange();
  Assert.assertEquals(0, range.getStart().getLine());
  Assert.assertEquals(4, range.getStart().getCharacter());
  Assert.assertEquals(1, range.getEnd().getLine());
  Assert.assertEquals(0, range.getEnd().getCharacter());
}
 
Example 5
Source File: cvc_complex_type_3_2_2CodeAction.java    From lemminx with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void doCodeAction(Diagnostic diagnostic, Range range, DOMDocument document, List<CodeAction> codeActions,
		SharedSettings sharedSettings, IComponentProvider componentProvider) {
	Range diagnosticRange = diagnostic.getRange();
	try {
		int offset = document.offsetAt(diagnosticRange.getEnd());
		DOMAttr attr = document.findAttrAt(offset);
		if (attr != null) {
			// Remove attribute
			int startOffset = attr.getStart();
			int endOffset = attr.getEnd();
			Range attrRange = new Range(document.positionAt(startOffset), document.positionAt(endOffset));
			CodeAction removeAttributeAction = CodeActionFactory.remove("Remove '" + attr.getName() + "' attribute",
					attrRange, document.getTextDocument(), diagnostic);
			codeActions.add(removeAttributeAction);
		}
	} catch (BadLocationException e) {
		// Do nothing
	}
}
 
Example 6
Source File: EqRequiredInAttributeCodeAction.java    From lemminx with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void doCodeAction(Diagnostic diagnostic, Range range, DOMDocument document, List<CodeAction> codeActions,
		SharedSettings sharedSettings, IComponentProvider componentProvider) {
	Range diagnosticRange = diagnostic.getRange();

	// Insert =""
	try {
		int offset = document.offsetAt(range.getStart());
		DOMNode node = document.findNodeAt(offset);
		if (node != null && node.isElement()) {
			String tagName = ((DOMElement) node).getTagName();
			if (tagName != null) {
				String insertText = "=\"\"";
				CodeAction insertEqualsAndQuotesAction = CodeActionFactory.insert("Insert '" + insertText + "'",
						diagnosticRange.getEnd(), insertText, document.getTextDocument(), diagnostic);
				codeActions.add(insertEqualsAndQuotesAction);
			}
		}
	} catch (BadLocationException e) {
		// do nothing
	}
}
 
Example 7
Source File: ValidatorTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testDiagnosticAtEndOfLineExcludingNewline() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("type lowercase");
  _builder.newLine();
  _builder.append("{");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  this.writeFile("MyType1.testlang", Strings.toUnixLineSeparator(_builder));
  this.initialize();
  final List<Diagnostic> problems = IterableExtensions.<Map.Entry<String, List<Diagnostic>>>head(this.getDiagnostics().entrySet()).getValue();
  String _join = IterableExtensions.join(problems, "\n");
  String _plus = ("problems found:\n" + _join);
  Assert.assertEquals(_plus, 1, problems.size());
  final Diagnostic problem = IterableExtensions.<Diagnostic>head(problems);
  this.assertEquals("Name should start with a capital", problem.getMessage());
  Assert.assertEquals(TestLanguageValidator.INVALID_NAME, problem.getCode().get());
  final Range range = problem.getRange();
  Assert.assertEquals(0, range.getStart().getLine());
  Assert.assertEquals(5, range.getStart().getCharacter());
  Assert.assertEquals(0, range.getEnd().getLine());
  Assert.assertEquals(14, range.getEnd().getCharacter());
}
 
Example 8
Source File: CamelDiagnosticTest.java    From camel-language-server with Apache License 2.0 5 votes vote down vote up
@Test
void testInvalidEnum() throws Exception {
	testDiagnostic("camel-with-invalid-enum", 1, ".xml");
	Diagnostic diagnostic = lastPublishedDiagnostics.getDiagnostics().get(0);
	assertThat(diagnostic.getMessage()).isNotNull();
	Range range = diagnostic.getRange();
	checkRange(range, 9, 49, 9, 54);
}
 
Example 9
Source File: N4JSCodeActionService.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private Options createOptions(XtextResource res, XDocument doc, TextDocumentIdentifier docIdentifier,
		LSPIssue issue, CancelIndicator cancelIndicator) {
	Diagnostic diagnostic = diagnosticIssueConverter.toDiagnostic(issue);
	CodeActionContext context = new CodeActionContext(Collections.singletonList(diagnostic));
	CodeActionParams codeActionParams = new CodeActionParams(docIdentifier, diagnostic.getRange(), context);
	Options newOptions = languageServer.toOptions(codeActionParams, doc, res, cancelIndicator);
	return newOptions;
}
 
Example 10
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 11
Source File: CamelPropertiesDiagnosticTest.java    From camel-language-server with Apache License 2.0 5 votes vote down vote up
@Test
void testUnknownParameterPropertiesFile() throws Exception {
	testDiagnostic("camel-with-unknownParameter", 1);
	Diagnostic diagnostic = lastPublishedDiagnostics.getDiagnostics().get(0);
	Range range1 = diagnostic.getRange();
	checkRange(range1, 0, 22, 0, 38);
	assertThat(diagnostic.getMessage()).isEqualTo("Unknown option");
}
 
Example 12
Source File: CamelDiagnosticTest.java    From camel-language-server with Apache License 2.0 5 votes vote down vote up
@Test
void testValidationErrorWithSyntaxError() throws Exception {
	testDiagnostic("camel-with-endpoint-error-withampersand", 1, ".xml");
	Diagnostic diagnostic = lastPublishedDiagnostics.getDiagnostics().get(0);
	assertThat(diagnostic.getMessage()).isNotNull();
	Range range = diagnostic.getRange();
	checkRange(range, 8, 16, 8, 47);
}
 
Example 13
Source File: CamelDiagnosticTest.java    From camel-language-server with Apache License 2.0 5 votes vote down vote up
@Test
void testInvalidEnumWithSameStringOnSameLine() throws Exception {
	testDiagnostic("camel-with-invalid-enum-with-same-string-in-camel-uri", 1, ".xml");
	Diagnostic diagnostic = lastPublishedDiagnostics.getDiagnostics().get(0);
	assertThat(diagnostic.getMessage()).isNotNull();
	Range range = diagnostic.getRange();
	checkRange(range, 9, 56, 9, 72);
}
 
Example 14
Source File: MavenJavaDiagnosticsMicroProfileHealthTest.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
private MicroProfileJavaCodeActionParams createCodeActionParams(String uri, Diagnostic d) {
	TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
	Range range = d.getRange();
	CodeActionContext context = new CodeActionContext();
	context.setDiagnostics(Arrays.asList(d));
	MicroProfileJavaCodeActionParams codeActionParams = new MicroProfileJavaCodeActionParams(textDocument, range,
			context);
	codeActionParams.setResourceOperationSupported(true);
	return codeActionParams;
}
 
Example 15
Source File: AbstractQuickfix.java    From camel-language-server with Apache License 2.0 5 votes vote down vote up
protected CodeAction createCodeAction(CodeActionParams params, Diagnostic diagnostic, String possibleProperty) {
	CodeAction codeAction = new CodeAction("Did you mean "+possibleProperty + "?");
	codeAction.setDiagnostics(Collections.singletonList(diagnostic));
	codeAction.setKind(CodeActionKind.QuickFix);
	Map<String, List<TextEdit>> changes = new HashMap<>();
	TextEdit textEdit = new TextEdit(diagnostic.getRange(), possibleProperty);
	changes.put(params.getTextDocument().getUri(), Arrays.asList(textEdit));
	codeAction.setEdit(new WorkspaceEdit(changes));
	return codeAction;
}
 
Example 16
Source File: AbstractQuickfix.java    From camel-language-server with Apache License 2.0 5 votes vote down vote up
protected String retrieveCurrentErrorValue(TextDocumentItem openedDocument, Diagnostic diagnostic) {
	Range diagnosticRange = diagnostic.getRange();
	String line = new ParserFileHelperUtil().getLine(openedDocument, diagnosticRange.getStart().getLine());
	int endCharacter = diagnosticRange.getEnd().getCharacter();
	if (line.length() > endCharacter) {
		return line.substring(diagnosticRange.getStart().getCharacter(), endCharacter);
	} else {
		return null;
	}
}
 
Example 17
Source File: cvc_attribute_3CodeAction.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doCodeAction(Diagnostic diagnostic, Range range, DOMDocument document, List<CodeAction> codeActions,
		SharedSettings sharedSettings, IComponentProvider componentProvider) {
	try {
		Range diagnosticRange = diagnostic.getRange();
		int offset = document.offsetAt(range.getStart());
		DOMAttr attr = document.findAttrAt(offset);
		if (attr != null) {
			DOMElement element = attr.getOwnerElement();
			String attributeName = attr.getName();
			ContentModelManager contentModelManager = componentProvider.getComponent(ContentModelManager.class);
			Collection<CMDocument> cmDocuments = contentModelManager.findCMDocument(element);
			for (CMDocument cmDocument : cmDocuments) {
				CMAttributeDeclaration cmAttribute = cmDocument.findCMAttribute(element, attributeName);
				if (cmAttribute != null) {
					Range rangeValue = new Range(
							new Position(diagnosticRange.getStart().getLine(),
									diagnosticRange.getStart().getCharacter() + 1),
							new Position(diagnosticRange.getEnd().getLine(),
									diagnosticRange.getEnd().getCharacter() - 1));
					cmAttribute.getEnumerationValues().forEach(value -> {
						// Replace attribute value
						// value = "${1:" + value + "}";
						CodeAction replaceAttrValueAction = CodeActionFactory.replace(
								"Replace with '" + value + "'", rangeValue, value, document.getTextDocument(),
								diagnostic);
						codeActions.add(replaceAttrValueAction);
					});
				}
			}
		}
	} catch (Exception e) {
		// do nothing
	}
}
 
Example 18
Source File: ElementDeclUnterminatedCodeAction.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doCodeAction(Diagnostic diagnostic, Range range, DOMDocument document, List<CodeAction> codeActions,
		SharedSettings sharedSettings, IComponentProvider componentProvider) {
	Range diagnosticRange = diagnostic.getRange();

	// Close with '>
	CodeAction closeAction = CodeActionFactory.insert("Close with '>'", diagnosticRange.getEnd(), ">",
			document.getTextDocument(), diagnostic);
	codeActions.add(closeAction);
}
 
Example 19
Source File: cvc_complex_type_2_3CodeAction.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doCodeAction(Diagnostic diagnostic, Range range, DOMDocument document, List<CodeAction> codeActions,
		SharedSettings sharedSettings, IComponentProvider componentProvider) {
	// Remove content
	Range diagnosticRange = diagnostic.getRange();
	CodeAction removeContentAction = CodeActionFactory.remove("Remove content", diagnosticRange,
			document.getTextDocument(), diagnostic);
	codeActions.add(removeContentAction);
}
 
Example 20
Source File: WorkspaceDiagnosticsHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testToDiagnosticsArray() throws Exception {
	String msg1 = "Something's wrong Jim";
	IMarker m1 = createMarker(IMarker.SEVERITY_WARNING, msg1, 2, 95, 100);

	String msg2 = "He's dead";
	IMarker m2 = createMarker(IMarker.SEVERITY_ERROR, msg2, 10, 1015, 1025);

	String msg3 = "It's probably time to panic";
	IMarker m3 = createMarker(42, msg3, 100, 10000, 10005);

	IDocument d = mock(IDocument.class);
	when(d.getLineOffset(1)).thenReturn(90);
	when(d.getLineOffset(9)).thenReturn(1000);
	when(d.getLineOffset(99)).thenReturn(10000);

	List<Diagnostic> diags = WorkspaceDiagnosticsHandler.toDiagnosticsArray(d, new IMarker[] { m1, m2, m3 }, true);
	assertEquals(3, diags.size());

	Range r;
	Diagnostic d1 = diags.get(0);
	assertEquals(msg1, d1.getMessage());
	assertEquals(DiagnosticSeverity.Warning, d1.getSeverity());
	r = d1.getRange();
	assertEquals(1, r.getStart().getLine());
	assertEquals(5, r.getStart().getCharacter());
	assertEquals(1, r.getEnd().getLine());
	assertEquals(10, r.getEnd().getCharacter());

	Diagnostic d2 = diags.get(1);
	assertEquals(msg2, d2.getMessage());
	assertEquals(DiagnosticSeverity.Error, d2.getSeverity());
	r = d2.getRange();
	assertEquals(9, r.getStart().getLine());
	assertEquals(15, r.getStart().getCharacter());
	assertEquals(9, r.getEnd().getLine());
	assertEquals(25, r.getEnd().getCharacter());

	Diagnostic d3 = diags.get(2);
	assertEquals(msg3, d3.getMessage());
	assertEquals(DiagnosticSeverity.Information, d3.getSeverity());
	r = d3.getRange();
	assertEquals(99, r.getStart().getLine());
	assertEquals(0, r.getStart().getCharacter());
	assertEquals(99, r.getEnd().getLine());
	assertEquals(5, r.getEnd().getCharacter());

}