@Test(dependsOnMethods = "testGetFileContent") public void testUpdateFile() throws IOException, WorkspaceDocumentException { String updateContent = readAll(filePath.toFile()) + "\nfunction foo(){\n}\n"; Optional<Lock> lock = documentManager.lockFile(filePath); try { TextDocumentContentChangeEvent changeEvent = new TextDocumentContentChangeEvent(updateContent); documentManager.updateFile(filePath, Collections.singletonList(changeEvent)); } finally { lock.ifPresent(Lock::unlock); } String actualContent = documentManager.getFileContent(filePath); Assert.assertEquals(actualContent, updateContent); }
@Override public void updateFile(Path filePath, List<TextDocumentContentChangeEvent> changeEvent) throws WorkspaceDocumentException { if (!isFileOpen(filePath)) { throw new WorkspaceDocumentException("File " + filePath.toString() + " is not opened in document manager."); } if (changeEvent.size() == 1 && changeEvent.get(0).getRange() == null) { documentList.get(filePath).getDocument().ifPresent( document -> document.setContent(changeEvent.get(0).getText()) ); } else { documentList.get(filePath).getDocument().ifPresent(document -> { TextEdit[] edits = new TextEdit[changeEvent.size()]; TextDocument textDocument = document.getTree().textDocument(); for (int i = 0; i < changeEvent.size(); i++) { TextDocumentContentChangeEvent change = changeEvent.get(i); edits[i] = TextEdit.from(getTextRange(textDocument, change), change.getText()); } document.setTree(SyntaxTree.from(document.getTree(), TextDocumentChange.from(edits))); }); } }
WorkspaceDocumentManagerImpl implements WorkspaceDocumentManager { @Override public void updateFile(Path filePath, List<TextDocumentContentChangeEvent> changeEvent) throws WorkspaceDocumentException { if (!isFileOpen(filePath)) { throw new WorkspaceDocumentException("File " + filePath.toString() + " is not opened in document manager."); } if (changeEvent.size() == 1 && changeEvent.get(0).getRange() == null) { documentList.get(filePath).getDocument().ifPresent( document -> document.setContent(changeEvent.get(0).getText()) ); } else { documentList.get(filePath).getDocument().ifPresent(document -> { TextEdit[] edits = new TextEdit[changeEvent.size()]; TextDocument textDocument = document.getTree().textDocument(); for (int i = 0; i < changeEvent.size(); i++) { TextDocumentContentChangeEvent change = changeEvent.get(i); edits[i] = TextEdit.from(getTextRange(textDocument, change), change.getText()); } document.setTree(SyntaxTree.from(document.getTree(), TextDocumentChange.from(edits))); }); } } }
WorkspaceDocumentManagerImpl implements WorkspaceDocumentManager { @Override public void updateFile(Path filePath, List<TextDocumentContentChangeEvent> changeEvent) throws WorkspaceDocumentException { if (!isFileOpen(filePath)) { throw new WorkspaceDocumentException("File " + filePath.toString() + " is not opened in document manager."); } if (changeEvent.size() == 1 && changeEvent.get(0).getRange() == null) { documentList.get(filePath).getDocument().ifPresent( document -> document.setContent(changeEvent.get(0).getText()) ); } else { documentList.get(filePath).getDocument().ifPresent(document -> { TextEdit[] edits = new TextEdit[changeEvent.size()]; TextDocument textDocument = document.getTree().textDocument(); for (int i = 0; i < changeEvent.size(); i++) { TextDocumentContentChangeEvent change = changeEvent.get(i); edits[i] = TextEdit.from(getTextRange(textDocument, change), change.getText()); } document.setTree(SyntaxTree.from(document.getTree(), TextDocumentChange.from(edits))); }); } } protected  WorkspaceDocumentManagerImpl(); }
WorkspaceDocumentManagerImpl implements WorkspaceDocumentManager { @Override public void updateFile(Path filePath, List<TextDocumentContentChangeEvent> changeEvent) throws WorkspaceDocumentException { if (!isFileOpen(filePath)) { throw new WorkspaceDocumentException("File " + filePath.toString() + " is not opened in document manager."); } if (changeEvent.size() == 1 && changeEvent.get(0).getRange() == null) { documentList.get(filePath).getDocument().ifPresent( document -> document.setContent(changeEvent.get(0).getText()) ); } else { documentList.get(filePath).getDocument().ifPresent(document -> { TextEdit[] edits = new TextEdit[changeEvent.size()]; TextDocument textDocument = document.getTree().textDocument(); for (int i = 0; i < changeEvent.size(); i++) { TextDocumentContentChangeEvent change = changeEvent.get(i); edits[i] = TextEdit.from(getTextRange(textDocument, change), change.getText()); } document.setTree(SyntaxTree.from(document.getTree(), TextDocumentChange.from(edits))); }); } } protected  WorkspaceDocumentManagerImpl(); static WorkspaceDocumentManagerImpl getInstance(); @Override boolean isFileOpen(Path filePath); @Override void openFile(Path filePath, String content); @Override void updateFile(Path filePath, List<TextDocumentContentChangeEvent> changeEvent); @Override void setCodeLenses(Path filePath, List<CodeLens> codeLens); @Override void closeFile(Path filePath); @Override List<CodeLens> getCodeLenses(Path filePath); @Override LSDocumentIdentifier getLSDocument(Path filePath); @Override String getFileContent(Path filePath); @Override Optional<Lock> lockFile(Path filePath); @Override Set<Path> getAllFilePaths(); @Override void clearAllFilePaths(); @Override SyntaxTree getTree(Path filePath); @Override void setTree(Path filePath, SyntaxTree newTree); }
WorkspaceDocumentManagerImpl implements WorkspaceDocumentManager { @Override public void updateFile(Path filePath, List<TextDocumentContentChangeEvent> changeEvent) throws WorkspaceDocumentException { if (!isFileOpen(filePath)) { throw new WorkspaceDocumentException("File " + filePath.toString() + " is not opened in document manager."); } if (changeEvent.size() == 1 && changeEvent.get(0).getRange() == null) { documentList.get(filePath).getDocument().ifPresent( document -> document.setContent(changeEvent.get(0).getText()) ); } else { documentList.get(filePath).getDocument().ifPresent(document -> { TextEdit[] edits = new TextEdit[changeEvent.size()]; TextDocument textDocument = document.getTree().textDocument(); for (int i = 0; i < changeEvent.size(); i++) { TextDocumentContentChangeEvent change = changeEvent.get(i); edits[i] = TextEdit.from(getTextRange(textDocument, change), change.getText()); } document.setTree(SyntaxTree.from(document.getTree(), TextDocumentChange.from(edits))); }); } } protected  WorkspaceDocumentManagerImpl(); static WorkspaceDocumentManagerImpl getInstance(); @Override boolean isFileOpen(Path filePath); @Override void openFile(Path filePath, String content); @Override void updateFile(Path filePath, List<TextDocumentContentChangeEvent> changeEvent); @Override void setCodeLenses(Path filePath, List<CodeLens> codeLens); @Override void closeFile(Path filePath); @Override List<CodeLens> getCodeLenses(Path filePath); @Override LSDocumentIdentifier getLSDocument(Path filePath); @Override String getFileContent(Path filePath); @Override Optional<Lock> lockFile(Path filePath); @Override Set<Path> getAllFilePaths(); @Override void clearAllFilePaths(); @Override SyntaxTree getTree(Path filePath); @Override void setTree(Path filePath, SyntaxTree newTree);  }