Search in sources :

Example 1 with DocumentHandle

use of org.eclipse.che.ide.api.editor.document.DocumentHandle in project che by eclipse.

the class LanguageServerAnnotationModel method createPositionFromDiagnostic.

protected Position createPositionFromDiagnostic(final DiagnosticDTO diagnostic) {
    DocumentHandle documentHandle = getDocumentHandle();
    Document document = documentHandle.getDocument();
    RangeDTO range = diagnostic.getRange();
    int start = document.getIndexFromPosition(new TextPosition(range.getStart().getLine(), range.getStart().getCharacter()));
    int end = document.getIndexFromPosition(new TextPosition(range.getEnd().getLine(), range.getEnd().getCharacter()));
    if (start == -1 && end == -1) {
        return new Position(0);
    }
    if (start == -1) {
        return new Position(end);
    }
    if (end == -1) {
        return new Position(start);
    }
    int length = end - start;
    if (length < 0) {
        return null;
    }
    return new Position(start, length);
}
Also used : DocumentHandle(org.eclipse.che.ide.api.editor.document.DocumentHandle) TextPosition(org.eclipse.che.ide.api.editor.text.TextPosition) Position(org.eclipse.che.ide.api.editor.text.Position) TextPosition(org.eclipse.che.ide.api.editor.text.TextPosition) Document(org.eclipse.che.ide.api.editor.document.Document) RangeDTO(org.eclipse.che.api.languageserver.shared.lsapi.RangeDTO)

Example 2 with DocumentHandle

use of org.eclipse.che.ide.api.editor.document.DocumentHandle in project che by eclipse.

the class EditorGroupSynchronizationImpl method updateContent.

private void updateContent(String newContent, String oldStamp, VirtualFile virtualFile) {
    final DocumentHandle documentHandle = getDocumentHandleFor(groupLeaderEditor);
    if (documentHandle == null) {
        return;
    }
    final Document document = documentHandle.getDocument();
    final String oldContent = document.getContents();
    final TextPosition cursorPosition = document.getCursorPosition();
    if (!(virtualFile instanceof File)) {
        replaceContent(document, newContent, oldContent, cursorPosition);
        return;
    }
    final File file = (File) virtualFile;
    final String newStamp = file.getModificationStamp();
    if (oldStamp == null && !Objects.equals(newContent, oldContent)) {
        replaceContent(document, newContent, oldContent, cursorPosition);
        return;
    }
    if (!Objects.equals(oldStamp, newStamp)) {
        replaceContent(document, newContent, oldContent, cursorPosition);
        notificationManager.notify("External operation", "File '" + file.getName() + "' is updated", SUCCESS, EMERGE_MODE);
    }
}
Also used : DocumentHandle(org.eclipse.che.ide.api.editor.document.DocumentHandle) TextPosition(org.eclipse.che.ide.api.editor.text.TextPosition) Document(org.eclipse.che.ide.api.editor.document.Document) File(org.eclipse.che.ide.api.resources.File) VirtualFile(org.eclipse.che.ide.api.resources.VirtualFile)

Example 3 with DocumentHandle

use of org.eclipse.che.ide.api.editor.document.DocumentHandle in project che by eclipse.

the class EditorGroupSynchronizationImpl method addEditor.

@Override
public void addEditor(EditorPartPresenter editor) {
    DocumentHandle documentHandle = getDocumentHandleFor(editor);
    if (documentHandle != null) {
        HandlerRegistration handlerRegistration = documentHandle.getDocEventBus().addHandler(DocumentChangeEvent.TYPE, this);
        synchronizedEditors.put(editor, handlerRegistration);
    }
}
Also used : HandlerRegistration(com.google.web.bindery.event.shared.HandlerRegistration) DocumentHandle(org.eclipse.che.ide.api.editor.document.DocumentHandle)

Example 4 with DocumentHandle

use of org.eclipse.che.ide.api.editor.document.DocumentHandle in project che by eclipse.

the class EditorGroupSynchronizationImplTest method shouldNotApplyChangesFromNotActiveEditor.

@Test
public void shouldNotApplyChangesFromNotActiveEditor() {
    DocumentHandle documentHandle1 = mock(DocumentHandle.class);
    when(documentChangeEvent.getDocument()).thenReturn(documentHandle1);
    when(documentHandle1.isSameAs(documentHandle)).thenReturn(false);
    editorGroupSynchronization.onDocumentChange(documentChangeEvent);
    verify(document, never()).replace(anyInt(), anyInt(), anyString());
}
Also used : DocumentHandle(org.eclipse.che.ide.api.editor.document.DocumentHandle) Test(org.junit.Test)

Example 5 with DocumentHandle

use of org.eclipse.che.ide.api.editor.document.DocumentHandle in project che by eclipse.

the class EditorGroupSynchronizationImplTest method shouldApplyChangesFromActiveEditor.

@Test
public void shouldApplyChangesFromActiveEditor() {
    int offset = 10;
    int removeCharCount = 100;
    String text = "someText";
    when(documentChangeEvent.getOffset()).thenReturn(offset);
    when(documentChangeEvent.getRemoveCharCount()).thenReturn(removeCharCount);
    when(documentChangeEvent.getText()).thenReturn(text);
    DocumentHandle documentHandle1 = mock(DocumentHandle.class);
    when(documentChangeEvent.getDocument()).thenReturn(documentHandle1);
    when(documentHandle1.isSameAs(documentHandle)).thenReturn(true);
    editorGroupSynchronization.onDocumentChange(documentChangeEvent);
    verify(document, times(2)).replace(eq(offset), eq(removeCharCount), eq(text));
}
Also used : DocumentHandle(org.eclipse.che.ide.api.editor.document.DocumentHandle) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

DocumentHandle (org.eclipse.che.ide.api.editor.document.DocumentHandle)6 Document (org.eclipse.che.ide.api.editor.document.Document)2 TextPosition (org.eclipse.che.ide.api.editor.text.TextPosition)2 Test (org.junit.Test)2 HandlerRegistration (com.google.web.bindery.event.shared.HandlerRegistration)1 RangeDTO (org.eclipse.che.api.languageserver.shared.lsapi.RangeDTO)1 Position (org.eclipse.che.ide.api.editor.text.Position)1 File (org.eclipse.che.ide.api.resources.File)1 VirtualFile (org.eclipse.che.ide.api.resources.VirtualFile)1 Matchers.anyString (org.mockito.Matchers.anyString)1