下面列出了怎么用org.eclipse.lsp4j.DidCloseTextDocumentParams的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void diagnosticsClose() 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);
DidCloseTextDocumentParams closeParams = new DidCloseTextDocumentParams();
closeParams.setTextDocument(new TextDocumentIdentifier());
closeParams.getTextDocument()
.setUri(RdfLintLanguageServer.convertFilePath2Uri(parentPath + "/needtrim.rdf"));
lsp.didClose(closeParams);
verify(client, times(2)).publishDiagnostics(any());
}
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();
}
@Override
public void didClose(DidCloseTextDocumentParams params) {
LOGGER.debug("didClose: {}", params.getTextDocument());
String uri = params.getTextDocument().getUri();
/*
* The rule observed by VS Code servers as explained in LSP specification is to
* clear the Diagnostic when it is related to a single file.
* https://microsoft.github.io/language-server-protocol/specification#
* textDocument_publishDiagnostics
*
* clear diagnostics before removing document.
*/
new DdlDiagnostics(this.teiidLanguageServer).clearDiagnostics(uri);
openedDocuments.remove(uri);
}
@Test
public void testDidClose() throws Exception {
URI fileURI = openFile("maven/salut4", "src/main/java/java/TestSyntaxError.java");
Job.getJobManager().join(SyntaxDocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, monitor);
String fileUri = ResourceUtils.fixURI(fileURI);
TextDocumentIdentifier identifier = new TextDocumentIdentifier(fileUri);
server.didClose(new DidCloseTextDocumentParams(identifier));
Job.getJobManager().join(SyntaxDocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, monitor);
List<PublishDiagnosticsParams> diagnosticReports = getClientRequests("publishDiagnostics");
assertEquals(2, diagnosticReports.size());
PublishDiagnosticsParams params = diagnosticReports.get(1);
assertEquals(fileUri, params.getUri());
assertNotNull(params.getDiagnostics());
assertTrue(params.getDiagnostics().isEmpty());
}
/**
* Notifies the server that the corresponding document has been closed
*/
public void documentClosed() {
pool(() -> {
if (this.isOpen) {
requestManager.didClose(new DidCloseTextDocumentParams(identifier));
isOpen = false;
EditorEventManagerBase.editorToManager.remove(editor);
EditorEventManagerBase.uriToManager.remove(FileUtils.editorToURIString(editor));
} else {
LOG.warn("Editor " + identifier.getUri() + " was already closed");
}
});
}
public void documentClosed() {
// When LS is shut down all documents are being disconnected. No need to send "didClose" message to the LS that is being shut down or not yet started
if (languageServerWrapper.isActive()) {
TextDocumentIdentifier identifier = new TextDocumentIdentifier(fileUri.toString());
DidCloseTextDocumentParams params = new DidCloseTextDocumentParams(identifier);
languageServerWrapper.getInitializedServer().thenAcceptAsync(ls -> ls.getTextDocumentService().didClose(params));
}
}
public T onDidCloseTextDocument(DidCloseTextDocumentParams params) {
synchronized (documents) {
T document = getDocument(params.getTextDocument());
if (document != null) {
documents.remove(params.getTextDocument().getUri());
}
return document;
}
}
@Override
public void didClose(DidCloseTextDocumentParams params) {
documents.onDidCloseTextDocument(params);
TextDocumentIdentifier document = params.getTextDocument();
String uri = document.getUri();
xmlLanguageServer.getLanguageClient()
.publishDiagnostics(new PublishDiagnosticsParams(uri, Collections.emptyList()));
getLimitExceededWarner().evictValue(uri);
}
@Override
public void didClose(DidCloseTextDocumentParams params) {
LOGGER.info("didClose: {}", params.getTextDocument());
String uri = params.getTextDocument().getUri();
openedDocuments.remove(uri);
/* The rule observed by VS Code servers as explained in LSP specification is to clear the Diagnostic when it is related to a single file.
* https://microsoft.github.io/language-server-protocol/specification#textDocument_publishDiagnostics
* */
new DiagnosticRunner(getCamelCatalog(), camelLanguageServer).clear(uri);
}
@Test
void testValidationErrorClearedOnClose() throws Exception {
testDiagnostic("camel-with-endpoint-error", 1, ".xml");
DidCloseTextDocumentParams params = new DidCloseTextDocumentParams(new TextDocumentIdentifier(DUMMY_URI+".xml"));
camelLanguageServer.getTextDocumentService().didClose(params);
await().timeout(AWAIT_TIMEOUT).untilAsserted(() -> assertThat(lastPublishedDiagnostics.getDiagnostics()).isEmpty());
}
@Override
public void didClose(DidCloseTextDocumentParams params) {
// remove source from map
sourceTextMap.remove(params.getTextDocument().getUri());
// clear diagnostics
PublishDiagnosticsParams diagnostics = new PublishDiagnosticsParams();
diagnostics.setUri(params.getTextDocument().getUri());
diagnostics.setDiagnostics(new LinkedList<>());
this.client.publishDiagnostics(diagnostics);
// diagnostics
diagnostics(params.getTextDocument().getUri());
}
public void didChangeWatchedFiles(DidChangeWatchedFilesParams param) {
List<FileEvent> changes = param.getChanges().stream().distinct().collect(Collectors.toList());
for (FileEvent fileEvent : changes) {
CHANGE_TYPE changeType = toChangeType(fileEvent.getType());
if (changeType == CHANGE_TYPE.DELETED) {
cleanUpDiagnostics(fileEvent.getUri());
handler.didClose(new DidCloseTextDocumentParams(new TextDocumentIdentifier(fileEvent.getUri())));
discardWorkingCopies(fileEvent.getUri());
}
ICompilationUnit unit = JDTUtils.resolveCompilationUnit(fileEvent.getUri());
if (unit != null && changeType == CHANGE_TYPE.CREATED && !unit.exists()) {
final ICompilationUnit[] units = new ICompilationUnit[1];
units[0] = unit;
try {
ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
@Override
public void run(IProgressMonitor monitor) throws CoreException {
units[0] = createCompilationUnit(units[0]);
}
}, new NullProgressMonitor());
} catch (CoreException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
unit = units[0];
}
if (unit != null) {
if (unit.isWorkingCopy()) {
continue;
}
if (changeType == CHANGE_TYPE.DELETED || changeType == CHANGE_TYPE.CHANGED) {
if (unit.equals(CoreASTProvider.getInstance().getActiveJavaElement())) {
CoreASTProvider.getInstance().disposeAST();
}
}
}
pm.fileChanged(fileEvent.getUri(), changeType);
}
}
public void didClose(DidCloseTextDocumentParams params) {
ISchedulingRule rule = JDTUtils.getRule(params.getTextDocument().getUri());
try {
ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
@Override
public void run(IProgressMonitor monitor) throws CoreException {
handleClosed(params);
}
}, rule, IWorkspace.AVOID_UPDATE, new NullProgressMonitor());
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Handle document close ", e);
}
}
public ICompilationUnit handleClosed(DidCloseTextDocumentParams params) {
String uri = params.getTextDocument().getUri();
ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri);
if (unit == null) {
return unit;
}
try {
synchronized (toReconcile) {
toReconcile.remove(unit);
}
if (isSyntaxMode(unit) || unit.getResource().isDerived()) {
createDiagnosticsHandler(unit).clearDiagnostics();
} else if (hasUnsavedChanges(unit)) {
unit.discardWorkingCopy();
unit.becomeWorkingCopy(new NullProgressMonitor());
publishDiagnostics(unit, new NullProgressMonitor());
}
if (unit.equals(sharedASTProvider.getActiveJavaElement())) {
sharedASTProvider.disposeAST();
}
unit.discardWorkingCopy();
if (JDTUtils.isDefaultProject(unit)) {
File f = new File(unit.getUnderlyingResource().getLocationURI());
if (!f.exists()) {
unit.delete(true, null);
}
}
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Error while handling document close. URI: " + uri, e);
}
return unit;
}
private void closeDocument(ICompilationUnit cu) {
DidCloseTextDocumentParams closeParms = new DidCloseTextDocumentParams();
TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
textDocument.setUri(JDTUtils.toURI(cu));
closeParms.setTextDocument(textDocument);
lifeCycleHandler.didClose(closeParms);
}
protected void close(final String fileUri) {
DidCloseTextDocumentParams _didCloseTextDocumentParams = new DidCloseTextDocumentParams();
final Procedure1<DidCloseTextDocumentParams> _function = (DidCloseTextDocumentParams it) -> {
TextDocumentIdentifier _textDocumentIdentifier = new TextDocumentIdentifier(fileUri);
it.setTextDocument(_textDocumentIdentifier);
};
DidCloseTextDocumentParams _doubleArrow = ObjectExtensions.<DidCloseTextDocumentParams>operator_doubleArrow(_didCloseTextDocumentParams, _function);
this.languageServer.didClose(_doubleArrow);
}
@Override
public void didClose(DidCloseTextDocumentParams params) {
fileContentsTracker.didClose(params);
URI uri = URI.create(params.getTextDocument().getUri());
compileAndVisitAST(uri);
}
public void didClose(DidCloseTextDocumentParams params) {
URI uri = URI.create(params.getTextDocument().getUri());
openFiles.remove(uri);
changedFiles.add(uri);
}
@Override
public void didClose(DidCloseTextDocumentParams params) {
openFilesManager.closeFile(getURI(params.getTextDocument()));
}
@Override
public void didClose(DidCloseTextDocumentParams params) {
openedDocuments.remove(params.getTextDocument().getUri());
}
@Override
public void close() {
this.opened = false;
this.requestManager.didClose(new DidCloseTextDocumentParams(new TextDocumentIdentifier(getURI())));
}
@Override
public void didClose(DidCloseTextDocumentParams params) {
logInfo(">> document/didClose");
documentLifeCycleHandler.didClose(params);
}
@Override
public ICompilationUnit handleClosed(DidCloseTextDocumentParams params) {
ICompilationUnit unit = super.handleClosed(params);
uninstallSemanticHighlightings(params.getTextDocument().getUri());
return unit;
}
@Override
public void didClose(DidCloseTextDocumentParams params) {
logInfo(">> document/didClose");
documentLifeCycleHandler.didClose(params);
}
/**
* Called when a file is closed in Visual Studio Code. We should no longer
* store the file as a String, and we can load the contents from the file
* system.
*/
@Override
public void didClose(DidCloseTextDocumentParams params)
{
TextDocumentIdentifier textDocument = params.getTextDocument();
String textDocumentUri = textDocument.getUri();
if (!textDocumentUri.endsWith(FILE_EXTENSION_AS)
&& !textDocumentUri.endsWith(FILE_EXTENSION_MXML))
{
//code intelligence is available only in .as and .mxml files
//so we ignore other file extensions
return;
}
Path path = LanguageServerCompilerUtils.getPathFromLanguageServerURI(textDocumentUri);
if (path == null)
{
return;
}
fileTracker.closeFile(path);
boolean clearProblems = false;
WorkspaceFolderData folderData = workspaceFolderManager.getWorkspaceFolderDataForSourceFile(path);
if (folderData == null)
{
//if we can't figure out which workspace the file is in, then clear
//the problems completely because we want to display problems only
//while it is open
clearProblems = true;
}
else
{
if (fallbackConfig != null && folderData.equals(workspaceFolderManager.getFallbackFolderData()))
{
fallbackConfig.didClose(path);
clearProblems = true;
}
getProject(folderData);
ILspProject project = folderData.project;
URI uri = path.toUri();
if(project == null)
{
//if the current project isn't properly configured, we want to
//display problems only while a file is open
clearProblems = true;
}
else if(notOnSourcePathSet.contains(uri))
{
//if the file is outside of the project's source path, we want
//to display problems only while it is open
clearProblems = true;
notOnSourcePathSet.remove(uri);
}
//if it's an included file, switch to the parent file
IncludeFileData includeFileData = folderData.includedFiles.get(path.toString());
if (includeFileData != null)
{
path = Paths.get(includeFileData.parentPath);
}
}
if (clearProblems)
{
//immediately clear any diagnostics published for this file
clearProblemsForURI(path.toUri());
return;
}
//the contents of the file may have been modified, and then reverted
//without saving changes, so re-check for errors with the file system
//version of the file
checkProjectForProblems(folderData);
}
@Override
public void didClose(DidCloseTextDocumentParams params) {
runBuildable(() -> toBuildable(params));
}
/**
* Evaluate the params and deduce the respective build command.
*/
protected Buildable toBuildable(DidCloseTextDocumentParams params) {
return workspaceManager.didClose(getURI(params.getTextDocument()));
}
@Override
public void didClose(DidCloseTextDocumentParams params) {
}
@Override
public void didClose(DidCloseTextDocumentParams params) {
System.out.println("didClose");
}
/**
* The document close notification is sent from the client to the server
* when the document got closed in the client. The document's truth now
* exists where the document's uri points to (e.g. if the document's uri is
* a file uri the truth now exists on disk).
*
* Registration Options: TextDocumentRegistrationOptions
*/
@JsonNotification
void didClose(DidCloseTextDocumentParams params);