Skip to content

Commit

Permalink
#1425 support all text document synchronization types, upgrade to nex…
Browse files Browse the repository at this point in the history
…t ls-api version
  • Loading branch information
Evgen Vidolob committed Aug 30, 2016
1 parent 0530607 commit be5d5d0
Show file tree
Hide file tree
Showing 32 changed files with 503 additions and 263 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import elemental.dom.Element;
import elemental.js.dom.JsElement;
import io.typefox.lsapi.DiagnosticSeverity;

import org.eclipse.che.ide.api.editor.text.annotation.Annotation;
import org.eclipse.che.ide.util.dom.Elements;
Expand Down Expand Up @@ -48,25 +49,25 @@ public DiagnosticAnnotation(DiagnosticDTO diagnostic) {

this.diagnostic = diagnostic;

Integer severity = diagnostic.getSeverity();
DiagnosticSeverity severity = diagnostic.getSeverity();
if (severity == null) {
layer = ERROR_LAYER;
setType(ERROR_ANNOTATION_TYPE);
} else {
switch (severity) {
case DiagnosticDTO.SEVERITY_ERROR:
case Error:
layer = ERROR_LAYER;
setType(ERROR_ANNOTATION_TYPE);
break;
case DiagnosticDTO.SEVERITY_WARNING:
case Warning:
layer = WARNING_LAYER;
setType(WARNING_ANNOTATION_TYPE);
break;
case DiagnosticDTO.SEVERITY_INFO:
case Information:
layer = INFO_LAYER;
setType(INFO_ANNOTATION_TYPE);
break;
case DiagnosticDTO.SEVERITY_HINT:
case Hint:
layer = HINT_LAYER;
setType(HINT_ANNOTATION_TYPE);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class LanguageServerEditorConfiguration extends DefaultTextEditorConfigur
public LanguageServerEditorConfiguration(final LanguageServerCodeassistProcessorFactory codeAssistProcessor,
final Provider<DocumentPositionMap> docPositionMapProvider,
final LanguageServerAnnotationModelFactory annotationModelFactory,
final Provider<LanguageServerReconcileStrategy> reconcileStrategyProvider,
final LanguageServerReconcileStrategyFactory reconcileStrategyProviderFactory,
final LanguageServerFormatterFactory formatterFactory,
@Assisted ServerCapabilities serverCapabilities) {
codeAssistProcessorFactory = codeAssistProcessor;
Expand All @@ -46,7 +46,7 @@ public LanguageServerEditorConfiguration(final LanguageServerCodeassistProcessor
this.annotationModel = annotationModelFactory.get(docPositionMapProvider.get());

this.reconciler = new ReconcilerWithAutoSave(DocumentPartitioner.DEFAULT_CONTENT_TYPE, getPartitioner());
reconciler.addReconcilingStrategy(DocumentPartitioner.DEFAULT_CONTENT_TYPE, reconcileStrategyProvider.get());
reconciler.addReconcilingStrategy(DocumentPartitioner.DEFAULT_CONTENT_TYPE, reconcileStrategyProviderFactory.build(serverCapabilities));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,85 +10,49 @@
*******************************************************************************/
package org.eclipse.che.plugin.languageserver.ide.editor;

import io.typefox.lsapi.ServerCapabilities;
import io.typefox.lsapi.TextDocumentSyncKind;

import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;

import org.eclipse.che.ide.api.editor.document.Document;
import org.eclipse.che.ide.api.editor.events.DocumentChangeEvent;
import org.eclipse.che.ide.api.editor.events.DocumentChangeHandler;
import org.eclipse.che.ide.api.editor.reconciler.DirtyRegion;
import org.eclipse.che.ide.api.editor.reconciler.ReconcilingStrategy;
import org.eclipse.che.ide.api.editor.text.Region;
import org.eclipse.che.ide.dto.DtoFactory;
import org.eclipse.che.plugin.languageserver.ide.service.TextDocumentServiceClient;
import org.eclipse.che.plugin.languageserver.shared.lsapi.DidChangeTextDocumentParamsDTO;
import org.eclipse.che.plugin.languageserver.shared.lsapi.TextDocumentContentChangeEventDTO;
import org.eclipse.che.plugin.languageserver.shared.lsapi.VersionedTextDocumentIdentifierDTO;

import java.util.Collections;
import org.eclipse.che.plugin.languageserver.ide.editor.sync.TextDocumentSynchronize;
import org.eclipse.che.plugin.languageserver.ide.editor.sync.TextDocumentSynchronizeFactory;

/**
* Responsible for document synchronization
*
* @author Evgen Vidolob
*/
public class LanguageServerReconcileStrategy implements ReconcilingStrategy {


private final TextDocumentServiceClient textDocumentService;
private final DtoFactory dtoFactory;

private int version = 0;
private final TextDocumentSynchronize synchronize;

@Inject
public LanguageServerReconcileStrategy(final TextDocumentServiceClient textDocumentService, final DtoFactory dtoFactory) {
this.textDocumentService = textDocumentService;
this.dtoFactory = dtoFactory;
public LanguageServerReconcileStrategy(TextDocumentSynchronizeFactory synchronizeFactory,
@Assisted ServerCapabilities serverCapabilities) {

TextDocumentSyncKind documentSync = serverCapabilities.getTextDocumentSync();
synchronize = synchronizeFactory.getSynchronize(documentSync);
}

@Override
public void setDocument(Document document) {
document.getDocumentHandle().getDocEventBus().addHandler(DocumentChangeEvent.TYPE, new DocumentChangeHandler() {
@Override
public void onDocumentChange(DocumentChangeEvent event) {
handleDocumentChange(event);
synchronize.syncTextDocument(event, ++version);
}
});
}

private void handleDocumentChange(DocumentChangeEvent event) {
Document document = event.getDocument().getDocument();
// TextPosition startPosition = document.getPositionFromIndex(event.getOffset());
// TextPosition endPosition;
// if (event.getRemoveCharCount() != 0) {
// endPosition = new TextPosition(startPosition.getLine(),startPosition.getCharacter()+ event.getRemoveCharCount());
// } else {
// endPosition = new TextPosition(startPosition.getLine(),startPosition.getCharacter()+ event.getLength());
// }

DidChangeTextDocumentParamsDTO changeDTO = dtoFactory.createDto(DidChangeTextDocumentParamsDTO.class);
String uri = document.getFile().getPath();
changeDTO.setUri(uri);
VersionedTextDocumentIdentifierDTO versionedDocId = dtoFactory.createDto(VersionedTextDocumentIdentifierDTO.class);
versionedDocId.setUri(uri);
versionedDocId.setVersion(++version);
changeDTO.setTextDocument(versionedDocId);
TextDocumentContentChangeEventDTO actualChange = dtoFactory.createDto(TextDocumentContentChangeEventDTO.class);
//TODO for now all vscode LS uses TextDocumentSyncKind#Full by default, we need to support all Sync kind

// RangeDTO range = dtoFactory.createDto(RangeDTO.class);
// PositionDTO start = dtoFactory.createDto(PositionDTO.class);
// start.setLine(startPosition.getLine());
// start.setCharacter(startPosition.getCharacter());
// PositionDTO end = dtoFactory.createDto(PositionDTO.class);
// end.setLine(endPosition.getLine());
// end.setCharacter(endPosition.getCharacter());
// range.setStart(start);
// range.setEnd(end);
// actualChange.setRange(range);

actualChange.setText(event.getDocument().getDocument().getContents());
changeDTO.setContentChanges(Collections.singletonList(actualChange));
textDocumentService.didChange(changeDTO);
}

@Override
public void reconcile(DirtyRegion dirtyRegion, Region subRegion) {
doReconcile();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*******************************************************************************
* Copyright (c) 2012-2016 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.plugin.languageserver.ide.editor;

import io.typefox.lsapi.ServerCapabilities;

/**
* @author Evgen Vidolob
*/
public interface LanguageServerReconcileStrategyFactory {
LanguageServerReconcileStrategy build(ServerCapabilities serverCapabilities);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
*******************************************************************************/
package org.eclipse.che.plugin.languageserver.ide.editor.codeassist;

import io.typefox.lsapi.CompletionItemKind;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import org.eclipse.che.ide.api.icon.Icon;
import org.eclipse.che.plugin.languageserver.ide.LanguageServerResources;
import org.eclipse.che.plugin.languageserver.shared.lsapi.CompletionItemDTO;
import org.vectomatic.dom.svg.ui.SVGResource;

import java.util.HashMap;
Expand All @@ -29,33 +30,33 @@ public class CompletionImageProvider {

private final LanguageServerResources resources;

private Map<Integer, SVGResource> imageMap = new HashMap<>();
private Map<CompletionItemKind, SVGResource> imageMap = new HashMap<>();

@Inject
public CompletionImageProvider(LanguageServerResources resources) {
this.resources = resources;
//TODO add missed icons
//no icon for keyword kind
imageMap.put(CompletionItemDTO.KIND_TEXT, resources.textItem());
imageMap.put(CompletionItemDTO.KIND_METHOD, resources.methodItem());
// imageMap.put(CompletionItemDTO.KIND_FUNCTION, );
// imageMap.put(CompletionItemDTO.KIND_CONSTRUCTOR, );
imageMap.put(CompletionItemDTO.KIND_FIELD, resources.fieldItem());
imageMap.put(CompletionItemDTO.KIND_VARIABLE, resources.variableItem());
imageMap.put(CompletionItemDTO.KIND_CLASS, resources.classItem());
imageMap.put(CompletionItemDTO.KIND_INTERFACE, resources.interfaceItem());
imageMap.put(CompletionItemDTO.KIND_MODULE, resources.moduleItem());
imageMap.put(CompletionItemDTO.KIND_PROPERTY, resources.propertyItem());
// imageMap.put(CompletionItemDTO.KIND_UNIT, );
imageMap.put(CompletionItemDTO.KIND_VALUE, resources.valueItem());
imageMap.put(CompletionItemDTO.KIND_ENUM, resources.enumItem());
imageMap.put(CompletionItemDTO.KIND_SNIPPET, resources.snippetItem());
// imageMap.put(CompletionItemDTO.KIND_COLOR, );
imageMap.put(CompletionItemDTO.KIND_FILE, resources.fileItem());
// imageMap.put(CompletionItemDTO.KIND_REFERENCE, );
imageMap.put(CompletionItemKind.Text, resources.textItem());
imageMap.put(CompletionItemKind.Method, resources.methodItem());
// imageMap.put(CompletionItemKind.KIND_FUNCTION, );
// imageMap.put(CompletionItemKind.KIND_CONSTRUCTOR, );
imageMap.put(CompletionItemKind.Field, resources.fieldItem());
imageMap.put(CompletionItemKind.Variable, resources.variableItem());
imageMap.put(CompletionItemKind.Class, resources.classItem());
imageMap.put(CompletionItemKind.Interface, resources.interfaceItem());
imageMap.put(CompletionItemKind.Module, resources.moduleItem());
imageMap.put(CompletionItemKind.Property, resources.propertyItem());
// imageMap.put(CompletionItemKind.KIND_UNIT, );
imageMap.put(CompletionItemKind.Value, resources.valueItem());
imageMap.put(CompletionItemKind.Enum, resources.enumItem());
imageMap.put(CompletionItemKind.Snippet, resources.snippetItem());
// imageMap.put(CompletionItemKind.KIND_COLOR, );
imageMap.put(CompletionItemKind.File, resources.fileItem());
// imageMap.put(CompletionItemKind.KIND_REFERENCE, );
}

public Icon getIcon(Integer completionKind) {
public Icon getIcon(CompletionItemKind completionKind) {
return new Icon("", imageMap.get(completionKind));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*******************************************************************************
* Copyright (c) 2012-2016 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.plugin.languageserver.ide.editor.sync;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import org.eclipse.che.ide.api.editor.document.Document;
import org.eclipse.che.ide.api.editor.events.DocumentChangeEvent;
import org.eclipse.che.ide.dto.DtoFactory;
import org.eclipse.che.plugin.languageserver.ide.service.TextDocumentServiceClient;
import org.eclipse.che.plugin.languageserver.shared.lsapi.DidChangeTextDocumentParamsDTO;
import org.eclipse.che.plugin.languageserver.shared.lsapi.TextDocumentContentChangeEventDTO;
import org.eclipse.che.plugin.languageserver.shared.lsapi.VersionedTextDocumentIdentifierDTO;

import java.util.Collections;

/**
* Handles full text synchronization
*
* @author Evgen Vidolob
*/
@Singleton
class FullTextDocumentSynchronize implements TextDocumentSynchronize {

private final DtoFactory dtoFactory;
private final TextDocumentServiceClient textDocumentService;

@Inject
public FullTextDocumentSynchronize(DtoFactory dtoFactory, TextDocumentServiceClient textDocumentService) {
this.dtoFactory = dtoFactory;
this.textDocumentService = textDocumentService;
}

@Override
public void syncTextDocument(DocumentChangeEvent event, int version) {
Document document = event.getDocument().getDocument();

DidChangeTextDocumentParamsDTO changeDTO = dtoFactory.createDto(DidChangeTextDocumentParamsDTO.class);
String uri = document.getFile().getLocation().toString();
changeDTO.setUri(uri);
VersionedTextDocumentIdentifierDTO versionedDocId = dtoFactory.createDto(VersionedTextDocumentIdentifierDTO.class);
versionedDocId.setUri(uri);
versionedDocId.setVersion(version);
changeDTO.setTextDocument(versionedDocId);
TextDocumentContentChangeEventDTO actualChange = dtoFactory.createDto(TextDocumentContentChangeEventDTO.class);

actualChange.setText(event.getDocument().getDocument().getContents());
changeDTO.setContentChanges(Collections.singletonList(actualChange));
textDocumentService.didChange(changeDTO);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*******************************************************************************
* Copyright (c) 2012-2016 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.plugin.languageserver.ide.editor.sync;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import org.eclipse.che.ide.api.editor.document.Document;
import org.eclipse.che.ide.api.editor.events.DocumentChangeEvent;
import org.eclipse.che.ide.api.editor.text.TextPosition;
import org.eclipse.che.ide.dto.DtoFactory;
import org.eclipse.che.plugin.languageserver.ide.service.TextDocumentServiceClient;
import org.eclipse.che.plugin.languageserver.shared.lsapi.DidChangeTextDocumentParamsDTO;
import org.eclipse.che.plugin.languageserver.shared.lsapi.PositionDTO;
import org.eclipse.che.plugin.languageserver.shared.lsapi.RangeDTO;
import org.eclipse.che.plugin.languageserver.shared.lsapi.TextDocumentContentChangeEventDTO;
import org.eclipse.che.plugin.languageserver.shared.lsapi.VersionedTextDocumentIdentifierDTO;

import java.util.Collections;

/**
* Handles incremental text document update
*
* @author Evgen Vidolob
*/
@Singleton
class IncrementalTextDocumentSynchronize implements TextDocumentSynchronize {

private final DtoFactory dtoFactory;
private final TextDocumentServiceClient textDocumentService;

@Inject
public IncrementalTextDocumentSynchronize(DtoFactory dtoFactory, TextDocumentServiceClient textDocumentService) {
this.dtoFactory = dtoFactory;
this.textDocumentService = textDocumentService;
}

@Override
public void syncTextDocument(DocumentChangeEvent event, int version) {
Document document = event.getDocument().getDocument();
TextPosition startPosition = document.getPositionFromIndex(event.getOffset());
TextPosition endPosition;
if (event.getRemoveCharCount() != 0) {
endPosition = new TextPosition(startPosition.getLine(), startPosition.getCharacter() + event.getRemoveCharCount());
} else {
endPosition = new TextPosition(startPosition.getLine(), startPosition.getCharacter() + event.getLength());
}

DidChangeTextDocumentParamsDTO changeDTO = dtoFactory.createDto(DidChangeTextDocumentParamsDTO.class);
String uri = document.getFile().getLocation().toString();
changeDTO.setUri(uri);
VersionedTextDocumentIdentifierDTO versionedDocId = dtoFactory.createDto(VersionedTextDocumentIdentifierDTO.class);
versionedDocId.setUri(uri);
versionedDocId.setVersion(version);
changeDTO.setTextDocument(versionedDocId);

RangeDTO range = dtoFactory.createDto(RangeDTO.class);
PositionDTO start = dtoFactory.createDto(PositionDTO.class);
start.setLine(startPosition.getLine());
start.setCharacter(startPosition.getCharacter());
PositionDTO end = dtoFactory.createDto(PositionDTO.class);
end.setLine(endPosition.getLine());
end.setCharacter(endPosition.getCharacter());
range.setStart(start);
range.setEnd(end);

TextDocumentContentChangeEventDTO actualChange = dtoFactory.createDto(TextDocumentContentChangeEventDTO.class);
actualChange.setRange(range);
actualChange.setText(event.getText());

changeDTO.setContentChanges(Collections.singletonList(actualChange));
textDocumentService.didChange(changeDTO);
}
}
Loading

0 comments on commit be5d5d0

Please sign in to comment.