org.eclipse.lsp4j.VersionedTextDocumentIdentifier Java Examples

The following examples show how to use org.eclipse.lsp4j.VersionedTextDocumentIdentifier. 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: AbstractIdeTest.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
private void changeOpenedFile(FileURI fileURI, Function<String, String> modification,
		BiFunction<String, String, List<TextDocumentContentChangeEvent>> changeComputer) {
	if (!isOpen(fileURI)) {
		Assert.fail("file is not open: " + fileURI);
	}
	// 1) change in memory (i.e. in map 'openFiles')
	OpenFileInfo info = openFiles.get(fileURI);
	int oldVersion = info.version;
	String oldContent = info.content;
	int newVersion = oldVersion + 1;
	String newContent = modification.apply(oldContent);
	Assert.assertNotNull(newContent);
	info.version = newVersion;
	info.content = newContent;
	// 2) notify LSP server
	VersionedTextDocumentIdentifier docId = new VersionedTextDocumentIdentifier(fileURI.toString(), newVersion);
	List<TextDocumentContentChangeEvent> changes = changeComputer.apply(oldContent, newContent);
	DidChangeTextDocumentParams params = new DidChangeTextDocumentParams(docId, changes);
	languageServer.didChange(params);
}
 
Example #2
Source File: DocumentLifeCycleHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private void changeDocument(ICompilationUnit cu, String content, int version, Range range, int length) throws JavaModelException {
	DidChangeTextDocumentParams changeParms = new DidChangeTextDocumentParams();
	VersionedTextDocumentIdentifier textDocument = new VersionedTextDocumentIdentifier();
	textDocument.setUri(JDTUtils.toURI(cu));
	textDocument.setVersion(version);
	changeParms.setTextDocument(textDocument);
	TextDocumentContentChangeEvent event = new TextDocumentContentChangeEvent();
	if (range != null) {
		event.setRange(range);
		event.setRangeLength(length);
	}
	event.setText(content);
	List<TextDocumentContentChangeEvent> contentChanges = new ArrayList<>();
	contentChanges.add(event);
	changeParms.setContentChanges(contentChanges);
	lifeCycleHandler.didChange(changeParms);
}
 
Example #3
Source File: AbstractIdeTest.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
private void closeFile(FileURI fileURI, boolean discardChanges) {
	if (!isOpen(fileURI)) {
		Assert.fail("trying to close a file that is not open: " + fileURI);
	}
	boolean dirty = isDirty(fileURI);
	if (dirty && !discardChanges) {
		Assert.fail("trying to close a file with unsaved changes: " + fileURI);
	} else if (!dirty && discardChanges) {
		Assert.fail("no unsaved changes to discard in file: " + fileURI);
	}

	OpenFileInfo info = openFiles.remove(fileURI);
	if (dirty) {
		// when closing a file with unsaved changes, LSP clients send a 'textDocument/didChange' to bring its
		// content back to the content on disk
		String contentOnDisk = getContentOfFileOnDisk(fileURI);
		DidChangeTextDocumentParams params = new DidChangeTextDocumentParams(
				new VersionedTextDocumentIdentifier(fileURI.toString(), info.version + 1),
				Collections.singletonList(new TextDocumentContentChangeEvent(contentOnDisk)));
		languageServer.didChange(params);
	}
	languageServer.didClose(new DidCloseTextDocumentParams(new TextDocumentIdentifier(fileURI.toString())));
	joinServerRequests();
}
 
Example #4
Source File: CodeActionFactory.java    From lemminx with Eclipse Public License 2.0 6 votes vote down vote up
public static CodeAction replaceAt(String title, String replaceText, TextDocumentItem document,
		Diagnostic diagnostic, Collection<Range> ranges) {
	CodeAction insertContentAction = new CodeAction(title);
	insertContentAction.setKind(CodeActionKind.QuickFix);
	insertContentAction.setDiagnostics(Arrays.asList(diagnostic));

	VersionedTextDocumentIdentifier versionedTextDocumentIdentifier = new VersionedTextDocumentIdentifier(
			document.getUri(), document.getVersion());
	List<TextEdit> edits = new ArrayList<TextEdit>();
	for (Range range : ranges) {
		TextEdit edit = new TextEdit(range, replaceText);
		edits.add(edit);
	}
	TextDocumentEdit textDocumentEdit = new TextDocumentEdit(versionedTextDocumentIdentifier, edits);
	WorkspaceEdit workspaceEdit = new WorkspaceEdit(Collections.singletonList(Either.forLeft(textDocumentEdit)));

	insertContentAction.setEdit(workspaceEdit);
	return insertContentAction;
}
 
Example #5
Source File: CodeActionFactory.java    From lemminx with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Makes a CodeAction to create a file and add content to the file.
 * 
 * @param title      The displayed name of the CodeAction
 * @param docURI     The file to create
 * @param content    The text to put into the newly created document.
 * @param diagnostic The diagnostic that this CodeAction will fix
 */
public static CodeAction createFile(String title, String docURI, String content, Diagnostic diagnostic) {

	List<Either<TextDocumentEdit, ResourceOperation>> actionsToTake = new ArrayList<>(2);

	// 1. create an empty file
	actionsToTake.add(Either.forRight(new CreateFile(docURI, new CreateFileOptions(false, true))));

	// 2. update the created file with the given content
	VersionedTextDocumentIdentifier identifier = new VersionedTextDocumentIdentifier(docURI, 0);
	TextEdit te = new TextEdit(new Range(new Position(0, 0), new Position(0, 0)), content);
	actionsToTake.add(Either.forLeft(new TextDocumentEdit(identifier, Collections.singletonList(te))));

	WorkspaceEdit createAndAddContentEdit = new WorkspaceEdit(actionsToTake);

	CodeAction codeAction = new CodeAction(title);
	codeAction.setEdit(createAndAddContentEdit);
	codeAction.setDiagnostics(Collections.singletonList(diagnostic));
	codeAction.setKind(CodeActionKind.QuickFix);

	return codeAction;
}
 
Example #6
Source File: SemanticHighlightingTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
protected void changeDocument(ICompilationUnit unit, String content, int version, Range range, int length) {
	DidChangeTextDocumentParams changeParms = new DidChangeTextDocumentParams();
	VersionedTextDocumentIdentifier textDocument = new VersionedTextDocumentIdentifier();
	textDocument.setUri(JDTUtils.toURI(unit));
	textDocument.setVersion(version);
	changeParms.setTextDocument(textDocument);
	TextDocumentContentChangeEvent event = new TextDocumentContentChangeEvent();
	if (range != null) {
		event.setRange(range);
		event.setRangeLength(length);
	}
	event.setText(content);
	List<TextDocumentContentChangeEvent> contentChanges = new ArrayList<>();
	contentChanges.add(event);
	changeParms.setContentChanges(contentChanges);
	lifeCycleHandler.didChange(changeParms);
}
 
Example #7
Source File: CamelTextDocumentServiceTest.java    From camel-language-server with Apache License 2.0 6 votes vote down vote up
@Test
void testChangeEventUpdatesStoredText() throws Exception {
	CamelLanguageServer camelLanguageServer = initializeLanguageServer("<to uri=\"\" xmlns=\"http://camel.apache.org/schema/blueprint\"></to>\n");
	
	DidChangeTextDocumentParams changeEvent = new DidChangeTextDocumentParams();
	VersionedTextDocumentIdentifier textDocument = new VersionedTextDocumentIdentifier();
	textDocument.setUri(DUMMY_URI+".xml");
	changeEvent.setTextDocument(textDocument);
	TextDocumentContentChangeEvent contentChange = new TextDocumentContentChangeEvent("<to xmlns=\"http://camel.apache.org/schema/blueprint\" uri=\"\"></to>\n");
	changeEvent.setContentChanges(Collections.singletonList(contentChange));
	camelLanguageServer.getTextDocumentService().didChange(changeEvent);
	
	//check old position doesn't provide completion
	CompletableFuture<Either<List<CompletionItem>, CompletionList>> completionsAtOldPosition = getCompletionFor(camelLanguageServer, new Position(0, 11));
	assertThat(completionsAtOldPosition.get().getLeft()).isEmpty();
	
	//check new position provides completion
	CompletableFuture<Either<List<CompletionItem>, CompletionList>> completionsAtNewPosition = getCompletionFor(camelLanguageServer, new Position(0, 58));
	assertThat(completionsAtNewPosition.get().getLeft()).isNotEmpty();
	
}
 
Example #8
Source File: VersionedTextDocumentIdentifierTypeAdapter.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
public VersionedTextDocumentIdentifier read(final JsonReader in) throws IOException {
  JsonToken nextToken = in.peek();
  if (nextToken == JsonToken.NULL) {
  	return null;
  }
  
  VersionedTextDocumentIdentifier result = new VersionedTextDocumentIdentifier();
  in.beginObject();
  while (in.hasNext()) {
  	String name = in.nextName();
  	switch (name) {
  	case "version":
  		result.setVersion(readVersion(in));
  		break;
  	case "uri":
  		result.setUri(readUri(in));
  		break;
  	default:
  		in.skipValue();
  	}
  }
  in.endObject();
  return result;
}
 
Example #9
Source File: SemanticHighlightingTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
protected void changeDocument(ICompilationUnit unit, int version, TextDocumentContentChangeEvent event, TextDocumentContentChangeEvent... rest) {
	DidChangeTextDocumentParams changeParms = new DidChangeTextDocumentParams();
	VersionedTextDocumentIdentifier textDocument = new VersionedTextDocumentIdentifier();
	textDocument.setUri(JDTUtils.toURI(unit));
	textDocument.setVersion(version);
	changeParms.setTextDocument(textDocument);
	changeParms.setContentChanges(Lists.asList(event, rest));
	lifeCycleHandler.didChange(changeParms);
}
 
Example #10
Source File: BasicDocument.java    From MSPaintIDE with MIT License 5 votes vote down vote up
public BasicDocument(ImageClass imageClass, LanguageServerWrapper lsWrapper) {
    this.file = imageClass.getInputImage();
    this.imageClass = imageClass;
    this.lsWrapper = lsWrapper;
    this.requestManager = lsWrapper.getRequestManager();
    this.hiddenClone = new File(file.getAbsolutePath().replaceAll("\\.png$", ""));

    this.text = imageClass.getText();

    this.changesParams = new DidChangeTextDocumentParams(new VersionedTextDocumentIdentifier(),
            Collections.singletonList(new TextDocumentContentChangeEvent()));

    changesParams.getTextDocument().setUri(getURI());
}
 
Example #11
Source File: SemanticHighlightingService.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public List<Position> install(ICompilationUnit unit) throws JavaModelException, BadPositionCategoryException {
	if (enabled.get()) {
		List<HighlightedPositionCore> positions = calculateHighlightedPositions(unit, false);
		String uri = JDTUtils.getFileURI(unit.getResource());
		this.cache.put(uri, positions);
		if (!positions.isEmpty()) {
			IDocument document = JsonRpcHelpers.toDocument(unit.getBuffer());
			List<SemanticHighlightingInformation> infos = toInfos(document, positions);
			VersionedTextDocumentIdentifier textDocument = new VersionedTextDocumentIdentifier(uri, 1);
			notifyClient(textDocument, infos);
		}
		return ImmutableList.copyOf(positions);
	}
	return emptyList();
}
 
Example #12
Source File: SemanticHighlightingService.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public void update(VersionedTextDocumentIdentifier textDocument, List<HighlightedPositionDiffContext> diffContexts) throws BadLocationException, BadPositionCategoryException, JavaModelException {
	if (enabled.get() && !diffContexts.isEmpty()) {
		List<SemanticHighlightingInformation> deltaInfos = newArrayList();
		for (HighlightedPositionDiffContext context : diffContexts) {
			deltaInfos.addAll(diffCalculator.getDiffInfos(context));
		}
		if (!deltaInfos.isEmpty()) {
			notifyClient(textDocument, deltaInfos);
		}
	}
}
 
Example #13
Source File: VersionedTextDocumentIdentifierTypeAdapter.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
public void write(final JsonWriter out, final VersionedTextDocumentIdentifier value) throws IOException {
  if (value == null) {
  	out.nullValue();
  	return;
  }
  
  out.beginObject();
  out.name("version");
  writeVersion(out, value.getVersion());
  out.name("uri");
  writeUri(out, value.getUri());
  out.endObject();
}
 
Example #14
Source File: FileContentsTrackerTests.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
@Test
void testDidChangeWithoutRange() {
	DidOpenTextDocumentParams openParams = new DidOpenTextDocumentParams();
	openParams.setTextDocument(new TextDocumentItem("file.txt", "plaintext", 1, "hello world"));
	tracker.didOpen(openParams);
	DidChangeTextDocumentParams changeParams = new DidChangeTextDocumentParams();
	changeParams.setTextDocument(new VersionedTextDocumentIdentifier("file.txt", 2));
	TextDocumentContentChangeEvent changeEvent = new TextDocumentContentChangeEvent();
	changeEvent.setText("hi there");
	changeParams.setContentChanges(Collections.singletonList(changeEvent));
	tracker.didChange(changeParams);
	Assertions.assertEquals("hi there", tracker.getContents(URI.create("file.txt")));
}
 
Example #15
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void changeDocument(ICompilationUnit unit, String content, int version) throws JavaModelException {
	DidChangeTextDocumentParams changeParms = new DidChangeTextDocumentParams();
	VersionedTextDocumentIdentifier textDocument = new VersionedTextDocumentIdentifier();
	textDocument.setUri(JDTUtils.toURI(unit));
	textDocument.setVersion(version);
	changeParms.setTextDocument(textDocument);
	TextDocumentContentChangeEvent event = new TextDocumentContentChangeEvent();
	event.setText(content);
	List<TextDocumentContentChangeEvent> contentChanges = new ArrayList<>();
	contentChanges.add(event);
	changeParms.setContentChanges(contentChanges);
	lifeCycleHandler.didChange(changeParms);
}
 
Example #16
Source File: AbstractLanguageServerTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected String _toExpectation(final VersionedTextDocumentIdentifier v) {
  StringConcatenation _builder = new StringConcatenation();
  String _lastSegment = org.eclipse.emf.common.util.URI.createURI(v.getUri()).lastSegment();
  _builder.append(_lastSegment);
  _builder.append(" <");
  Integer _version = v.getVersion();
  _builder.append(_version);
  _builder.append(">");
  return _builder.toString();
}
 
Example #17
Source File: ChangeConverter2.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected Object addTextEdit(String theUri, Document document, TextEdit... textEdits) {
	if (useDocumentChanges) {
		TextDocumentEdit textDocumentEdit = new TextDocumentEdit();
		VersionedTextDocumentIdentifier versionedTextDocumentIdentifier = new VersionedTextDocumentIdentifier();
		versionedTextDocumentIdentifier.setUri(theUri);
		versionedTextDocumentIdentifier.setVersion(document.getVersion());
		textDocumentEdit.setTextDocument(versionedTextDocumentIdentifier);
		textDocumentEdit.setEdits(Arrays.asList(textEdits));
		return edit.getDocumentChanges().add(Either.forLeft(textDocumentEdit));
	} else {
		return edit.getChanges().put(theUri, Arrays.asList(textEdits));
	}
}
 
Example #18
Source File: SemanticHighlightingRegistry.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected VersionedTextDocumentIdentifier toVersionedTextDocumentIdentifier(final ILanguageServerAccess.Context context) {
  VersionedTextDocumentIdentifier _versionedTextDocumentIdentifier = new VersionedTextDocumentIdentifier();
  final Procedure1<VersionedTextDocumentIdentifier> _function = (VersionedTextDocumentIdentifier it) -> {
    it.setUri(this._uriExtensions.toUriString(context.getResource().getURI()));
    it.setVersion(context.getDocument().getVersion());
  };
  return ObjectExtensions.<VersionedTextDocumentIdentifier>operator_doubleArrow(_versionedTextDocumentIdentifier, _function);
}
 
Example #19
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(VersionedTextDocumentIdentifier textDocument) {
	if (textDocument == null) {
		return "";
	}
	String str = Strings.join(", ",
			textDocument.getUri(),
			textDocument.getVersion());

	return "(" + str + ")";
}
 
Example #20
Source File: AbstractIdeTest.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/** Save the given, open file's in-memory content to disk. Does *not* close the file. */
protected void saveOpenedFile(FileURI fileURI) {
	if (!isOpen(fileURI)) {
		Assert.fail("file is not open: " + fileURI);
	}
	OpenFileInfo info = openFiles.get(fileURI);
	// 1) save current content to disk
	changeFileOnDiskWithoutNotification(fileURI, info.content);
	// 2) notify LSP server
	VersionedTextDocumentIdentifier docId = new VersionedTextDocumentIdentifier(fileURI.toString(), info.version);
	DidSaveTextDocumentParams params = new DidSaveTextDocumentParams(docId);
	languageServer.didSave(params);
}
 
Example #21
Source File: RdfLintLanguageServerTest.java    From rdflint with MIT License 5 votes vote down vote up
@Test
public void diagnosticsChangeParseError() throws Exception {
  RdfLintLanguageServer lsp = new RdfLintLanguageServer();
  InitializeParams initParams = new InitializeParams();
  String rootPath = this.getClass().getClassLoader().getResource("testValidatorsImpl/").getPath();
  String parentPath = rootPath + "TrimValidator/turtle_needtrim";
  initParams.setRootUri(RdfLintLanguageServer.convertFilePath2Uri(parentPath));
  lsp.initialize(initParams);

  LanguageClient client = mock(LanguageClient.class);
  lsp.connect(client);

  DidChangeTextDocumentParams changeParams = new DidChangeTextDocumentParams();
  changeParams.setTextDocument(new VersionedTextDocumentIdentifier());
  changeParams.getTextDocument()
      .setUri(RdfLintLanguageServer.convertFilePath2Uri(parentPath + "/needtrim.rdf"));
  List<TextDocumentContentChangeEvent> changeEvents = new LinkedList<>();
  changeParams.setContentChanges(changeEvents);
  changeEvents.add(new TextDocumentContentChangeEvent());
  changeEvents.get(0).setText("<rdf:RDF\n"
      + "    xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n"
      + "    xmlns:schema=\"http://schema.org/\"\n"
      + "    >\n"
      + "\n"
      + "  <rdf:Description rdf:about=\"something\">\n"
      + "    <schema:familyName xml:lang=\"ja\">familyName</schema:familyN>\n"
      + "  </rdf:Description>\n"
      + "\n"
      + "</rdf:RDF>");
  lsp.didChange(changeParams);

  verify(client, times(1)).publishDiagnostics(any());
}
 
Example #22
Source File: RdfLintLanguageServerTest.java    From rdflint with MIT License 5 votes vote down vote up
@Test
public void diagnosticsChange() throws Exception {
  RdfLintLanguageServer lsp = new RdfLintLanguageServer();
  InitializeParams initParams = new InitializeParams();
  String rootPath = this.getClass().getClassLoader().getResource("testValidatorsImpl/").getPath();
  String parentPath = rootPath + "TrimValidator/turtle_needtrim";
  initParams.setRootUri(RdfLintLanguageServer.convertFilePath2Uri(parentPath));
  lsp.initialize(initParams);

  LanguageClient client = mock(LanguageClient.class);
  lsp.connect(client);

  DidChangeTextDocumentParams changeParams = new DidChangeTextDocumentParams();
  changeParams.setTextDocument(new VersionedTextDocumentIdentifier());
  changeParams.getTextDocument()
      .setUri(RdfLintLanguageServer.convertFilePath2Uri(parentPath + "/needtrim.rdf"));
  List<TextDocumentContentChangeEvent> changeEvents = new LinkedList<>();
  changeParams.setContentChanges(changeEvents);
  changeEvents.add(new TextDocumentContentChangeEvent());
  changeEvents.get(0).setText("<rdf:RDF\n"
      + "    xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n"
      + "    xmlns:schema=\"http://schema.org/\"\n"
      + "    >\n"
      + "\n"
      + "  <rdf:Description rdf:about=\"something\">\n"
      + "    <schema:familyName xml:lang=\"ja\">familyName </schema:familyName>\n"
      + "  </rdf:Description>\n"
      + "\n"
      + "</rdf:RDF>");
  lsp.didChange(changeParams);

  verify(client, times(1)).publishDiagnostics(any());
}
 
Example #23
Source File: CamelDiagnosticTest.java    From camel-language-server with Apache License 2.0 5 votes vote down vote up
@Test
void testValidationErrorUpdatedOnChange() throws Exception {
	testDiagnostic("camel-with-endpoint-error", 1, ".xml");
	
	camelLanguageServer.getTextDocumentService().getOpenedDocument(DUMMY_URI+".xml").getText();
	DidChangeTextDocumentParams params = new DidChangeTextDocumentParams();
	params.setTextDocument(new VersionedTextDocumentIdentifier(DUMMY_URI+".xml", 2));
	List<TextDocumentContentChangeEvent> contentChanges = new ArrayList<>();
	contentChanges.add(new TextDocumentContentChangeEvent("<from uri=\"timer:timerName?delay=1000\" xmlns=\"http://camel.apache.org/schema/blueprint\"></from>\n"));
	params.setContentChanges(contentChanges);
	camelLanguageServer.getTextDocumentService().didChange(params);
	
	await().timeout(AWAIT_TIMEOUT).untilAsserted(() -> assertThat(lastPublishedDiagnostics.getDiagnostics()).isEmpty());
}
 
Example #24
Source File: FileContentsTrackerTests.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
@Test
void testDidChangeWithRange() {
	DidOpenTextDocumentParams openParams = new DidOpenTextDocumentParams();
	openParams.setTextDocument(new TextDocumentItem("file.txt", "plaintext", 1, "hello world"));
	tracker.didOpen(openParams);
	DidChangeTextDocumentParams changeParams = new DidChangeTextDocumentParams();
	changeParams.setTextDocument(new VersionedTextDocumentIdentifier("file.txt", 2));
	TextDocumentContentChangeEvent changeEvent = new TextDocumentContentChangeEvent();
	changeEvent.setText(", friend");
	changeEvent.setRange(new Range(new Position(0, 5), new Position(0, 11)));
	changeEvent.setRangeLength(6);
	changeParams.setContentChanges(Collections.singletonList(changeEvent));
	tracker.didChange(changeParams);
	Assertions.assertEquals("hello, friend", tracker.getContents(URI.create("file.txt")));
}
 
Example #25
Source File: XMLAssert.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
public static CodeAction ca(Diagnostic d, TextEdit... te) {
	CodeAction codeAction = new CodeAction();
	codeAction.setTitle("");
	codeAction.setDiagnostics(Arrays.asList(d));

	VersionedTextDocumentIdentifier versionedTextDocumentIdentifier = new VersionedTextDocumentIdentifier(FILE_URI,
			0);

	TextDocumentEdit textDocumentEdit = new TextDocumentEdit(versionedTextDocumentIdentifier, Arrays.asList(te));
	WorkspaceEdit workspaceEdit = new WorkspaceEdit(Collections.singletonList(Either.forLeft(textDocumentEdit)));
	codeAction.setEdit(workspaceEdit);
	return codeAction;
}
 
Example #26
Source File: EditorEventManager.java    From lsp4intellij with Apache License 2.0 5 votes vote down vote up
public EditorEventManager(Editor editor, DocumentListener documentListener, EditorMouseListener mouseListener,
                          EditorMouseMotionListener mouseMotionListener, LSPCaretListenerImpl caretListener,
                          RequestManager requestManager, ServerOptions serverOptions, LanguageServerWrapper wrapper) {

    this.editor = editor;
    this.documentListener = documentListener;
    this.mouseListener = mouseListener;
    this.mouseMotionListener = mouseMotionListener;
    this.requestManager = requestManager;
    this.wrapper = wrapper;
    this.caretListener = caretListener;

    this.identifier = new TextDocumentIdentifier(FileUtils.editorToURIString(editor));
    this.changesParams = new DidChangeTextDocumentParams(new VersionedTextDocumentIdentifier(),
            Collections.singletonList(new TextDocumentContentChangeEvent()));
    this.syncKind = serverOptions.syncKind;

    this.completionTriggers = (serverOptions.completionOptions != null
            && serverOptions.completionOptions.getTriggerCharacters() != null) ?
            serverOptions.completionOptions.getTriggerCharacters() :
            new ArrayList<>();

    this.signatureTriggers = (serverOptions.signatureHelpOptions != null
            && serverOptions.signatureHelpOptions.getTriggerCharacters() != null) ?
            serverOptions.signatureHelpOptions.getTriggerCharacters() :
            new ArrayList<>();

    this.project = editor.getProject();

    EditorEventManagerBase.uriToManager.put(FileUtils.editorToURIString(editor), this);
    EditorEventManagerBase.editorToManager.put(editor, this);
    changesParams.getTextDocument().setUri(identifier.getUri());

    this.currentHint = null;
}
 
Example #27
Source File: DocumentContentSynchronizer.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Convert IntelliJ {@link DocumentEvent} to LS according {@link TextDocumentSyncKind}.
 *
 * @param event
 *            IntelliJ {@link DocumentEvent}
 * @return true if change event is ready to be sent
 */
private boolean createChangeEvent(DocumentEvent event) {
    changeParams = new DidChangeTextDocumentParams(new VersionedTextDocumentIdentifier(), Collections.singletonList(new TextDocumentContentChangeEvent()));
    changeParams.getTextDocument().setUri(fileUri.toString());

    Document document = event.getDocument();
    TextDocumentContentChangeEvent changeEvent = null;
    TextDocumentSyncKind syncKind = getTextDocumentSyncKind();
    switch (syncKind) {
        case None:
            return false;
        case Full:
            changeParams.getContentChanges().get(0).setText(event.getDocument().getText());
            break;
        case Incremental:
            changeEvent = changeParams.getContentChanges().get(0);
            CharSequence newText = event.getNewFragment();
            int offset = event.getOffset();
            int length = event.getOldLength();
            try {
                // try to convert the Eclipse start/end offset to LS range.
                Range range = new Range(LSPIJUtils.toPosition(offset, document),
                        LSPIJUtils.toPosition(offset + length, document));
                changeEvent.setRange(range);
                changeEvent.setText(newText.toString());
                changeEvent.setRangeLength(length);
            } finally {
            }
            break;
    }
    return true;
}
 
Example #28
Source File: FileContentsTrackerTests.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
@Test
void testDidChangeWithRangeMultiline() {
	DidOpenTextDocumentParams openParams = new DidOpenTextDocumentParams();
	openParams.setTextDocument(new TextDocumentItem("file.txt", "plaintext", 1, "hello\nworld"));
	tracker.didOpen(openParams);
	DidChangeTextDocumentParams changeParams = new DidChangeTextDocumentParams();
	changeParams.setTextDocument(new VersionedTextDocumentIdentifier("file.txt", 2));
	TextDocumentContentChangeEvent changeEvent = new TextDocumentContentChangeEvent();
	changeEvent.setText("affles");
	changeEvent.setRange(new Range(new Position(1, 1), new Position(1, 5)));
	changeEvent.setRangeLength(4);
	changeParams.setContentChanges(Collections.singletonList(changeEvent));
	tracker.didChange(changeParams);
	Assertions.assertEquals("hello\nwaffles", tracker.getContents(URI.create("file.txt")));
}
 
Example #29
Source File: SemanticHighlightingParams.java    From lsp4j with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * The text document that has to be decorated with the semantic highlighting information.
 */
public void setTextDocument(@NonNull final VersionedTextDocumentIdentifier textDocument) {
  this.textDocument = Preconditions.checkNotNull(textDocument, "textDocument");
}
 
Example #30
Source File: SemanticHighlightingParams.java    From lsp4j with Eclipse Public License 2.0 4 votes vote down vote up
public SemanticHighlightingParams(@NonNull final VersionedTextDocumentIdentifier textDocument, @NonNull final List<SemanticHighlightingInformation> lines) {
  this.textDocument = Preconditions.<VersionedTextDocumentIdentifier>checkNotNull(textDocument, "textDocument");
  this.lines = Preconditions.<List<SemanticHighlightingInformation>>checkNotNull(lines, "lines");
}