Skip to content

Commit 3fca5e0

Browse files
authored
Merge pull request #3628 from 1c-syntax/feature/handle-didChangeWatchedFiles
Обработка событий workspace/didChangeWatchedFiles
2 parents 68f0609 + 28c8487 commit 3fca5e0

File tree

12 files changed

+695
-38
lines changed

12 files changed

+695
-38
lines changed

docs/en/index.md

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,26 @@ Perfomance measurement - [SSL 3.1](../bench/index.html)
5454
## Supported protocol operations
5555

5656
??? workspace
57-
    | Operation                                                     | Support                                                    | Commentary                                                  |
58-
    | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
59-
    | [didChangeWorkspaceFolders](https://microsoft.github.io/language-server-protocol/specification-current#workspace_didChangeWorkspaceFolders) | <img src="./assets/images/cross.svg" alt="no" width="20">   |                                                              |
60-
    | [didChangeConfiguration](https://microsoft.github.io/language-server-protocol/specification#workspace_didChangeConfiguration) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | with restrictions see [#1431](https://github.com/1c-syntax/bsl-language-server/issues/1431) |
61-
    | [didChangeWatchedFiles](https://microsoft.github.io/language-server-protocol/specification#workspace_didChangeWatchedFiles) | <img src="./assets/images/cross.svg" alt="no" width="20">   |                                                              |
62-
    | [symbol](https://microsoft.github.io/language-server-protocol/specification#workspace_symbol) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> |                                                              |
63-
    | [executeCommand](https://microsoft.github.io/language-server-protocol/specification#workspace_executeCommand) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> |                                                              |
57+
| Operation | Support | Comment |
58+
| ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
59+
| [didChangeWorkspaceFolders](https://microsoft.github.io/language-server-protocol/specification-current#workspace_didChangeWorkspaceFolders) | <img src="./assets/images/cross.svg" alt="no" width="20"> | |
60+
| [didChangeConfiguration](https://microsoft.github.io/language-server-protocol/specification#workspace_didChangeConfiguration) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | with restrictions see [#1431](https://github.com/1c-syntax/bsl-language-server/issues/1431) |
61+
| [didChangeWatchedFiles](https://microsoft.github.io/language-server-protocol/specification#workspace_didChangeWatchedFiles) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | |
62+
| [symbol](https://microsoft.github.io/language-server-protocol/specification#workspace_symbol) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | |
63+
| [executeCommand](https://microsoft.github.io/language-server-protocol/specification#workspace_executeCommand) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | |
6464
| [diagnostic/refresh](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_diagnostic_refresh) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | sent on configuration change |
65-
    | [applyEdit](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_applyEdit) | <img src="./assets/images/cross.svg" alt="no" width="20">   |                                                              |
66-
    | [willCreateFiles](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_willCreateFiles) |   <img src="./assets/images/cross.svg" alt="no" width="20">  |                                                              |
65+
| [applyEdit](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_applyEdit) | <img src="./assets/images/cross.svg" alt="no" width="20"> | |
66+
| [willCreateFiles](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_willCreateFiles) | <img src="./assets/images/cross.svg" alt="no" width="20"> | |
6767

6868
??? "Text Synchronization"
69-
| Opertaion | Supported | Comment |
70-
| --------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------ |
71-
| [didOpen](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_didOpen) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | | |
72-
| [didChange](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_didChange) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | textDocumentSyncKind = Incremental | |
73-
| [didClose](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_didClose) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | | |
74-
| [didSave](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_didSave) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | | |
75-
| [willSave](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_willSave) | <img src="./assets/images/cross.svg" alt="no" width="20"> | | |
76-
| [willSaveWaitUntil](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_willSaveWaitUntil) | <img src="./assets/images/cross.svg" alt="no" width="20"> | | |
69+
| Operation | Supported | Comment |
70+
| --------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------ |
71+
| [didOpen](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_didOpen) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | | |
72+
| [didChange](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_didChange) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | textDocumentSyncKind = Incremental | |
73+
| [didClose](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_didClose) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | | |
74+
| [didSave](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_didSave) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | | |
75+
| [willSave](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_willSave) | <img src="./assets/images/cross.svg" alt="no" width="20"> | | |
76+
| [willSaveWaitUntil](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_willSaveWaitUntil) | <img src="./assets/images/cross.svg" alt="no" width="20"> | | |
7777

7878
??? textDocument
7979
| Operation | Support | Commentary | Is configured? |
@@ -262,14 +262,6 @@ Thanks to companies supporting open source projects, and especially to those who
262262
263263
---
264264
265-
[![YourKit](https://www.yourkit.com/images/yklogo.png)](https://www.yourkit.com)  
266-
267-
[YourKit, LLC](https://www.yourkit.com) is the creator of innovative and intelligent tools for profiling `Java` and `.NET` applications. YourKit has offered an open source license [YourKit Java Profiler](https://www.yourkit.com) for `BSL Language Server` to improve its performance.
268-
269-
With `YourKit Java Profiler` we profile and improve project performance.
270-
271-
---
272-
273265
[![JetBrains](assets/images/jetbrains-variant-4.png)](https://www.jetbrains.com?from=bsl-language-server)
274266
275267
[JetBrains](https://www.jetbrains.com?from=bsl-language-server) is the creator of professional software for development. JetBrains has offered an open source license for his products, including `IntelliJ IDEA Ultimate`.

docs/index.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
| ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
5959
| [didChangeWorkspaceFolders](https://microsoft.github.io/language-server-protocol/specification-current#workspace_didChangeWorkspaceFolders) | <img src="./assets/images/cross.svg" alt="no" width="20"> | |
6060
| [didChangeConfiguration](https://microsoft.github.io/language-server-protocol/specification#workspace_didChangeConfiguration) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | с ограничениями, см. [#1431](https://github.com/1c-syntax/bsl-language-server/issues/1431) |
61-
| [didChangeWatchedFiles](https://microsoft.github.io/language-server-protocol/specification#workspace_didChangeWatchedFiles) | <img src="./assets/images/cross.svg" alt="no" width="20"> | |
61+
| [didChangeWatchedFiles](https://microsoft.github.io/language-server-protocol/specification#workspace_didChangeWatchedFiles) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | |
6262
| [symbol](https://microsoft.github.io/language-server-protocol/specification#workspace_symbol) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | |
6363
| [executeCommand](https://microsoft.github.io/language-server-protocol/specification#workspace_executeCommand) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | |
6464
| [diagnostic/refresh](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_diagnostic_refresh) | <img src="./assets/images/checkmark.svg" alt="yes" width="20"> | отправляется при изменении конфигурации |
@@ -262,14 +262,6 @@ java -jar bsl-language-server.jar --format --src ./src/cf
262262
263263
---
264264
265-
[![YourKit](https://www.yourkit.com/images/yklogo.png)](https://www.yourkit.com)
266-
267-
Создатель инновационных и интеллектуальных инструментов для профилирования приложений `Java` и `.NET` [YourKit, LLC](https://www.yourkit.com) любезно предоставил нам лицензии на продукт `YourKit Java Profiler`.
268-
269-
С помощью `YourKit Java Profiler` мы мониторим и улучшаем производительность проекта.
270-
271-
---
272-
273265
[![JetBrains](assets/images/jetbrains-variant-4.png)](https://www.jetbrains.com?from=bsl-language-server)
274266
275267
Создатель профессиональных инструментов разработки программного обеспечения, инновационных и мощных, [JetBrains](https://www.jetbrains.com?from=bsl-language-server) поддержал наш проект, предоставив лицензии на свои продукты, в том числе на `IntelliJ IDEA Ultimate`.

src/main/java/com/github/_1c_syntax/bsl/languageserver/BSLWorkspaceService.java

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
package com.github._1c_syntax.bsl.languageserver;
2323

2424
import com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration;
25+
import com.github._1c_syntax.bsl.languageserver.context.ServerContext;
2526
import com.github._1c_syntax.bsl.languageserver.providers.CommandProvider;
2627
import com.github._1c_syntax.bsl.languageserver.providers.SymbolProvider;
28+
import com.github._1c_syntax.utils.Absolute;
2729
import jakarta.annotation.PreDestroy;
2830
import lombok.RequiredArgsConstructor;
2931
import org.apache.commons.beanutils.PropertyUtils;
@@ -39,6 +41,7 @@
3941
import org.springframework.stereotype.Component;
4042

4143
import java.lang.reflect.InvocationTargetException;
44+
import java.net.URI;
4245
import java.util.List;
4346
import java.util.concurrent.CompletableFuture;
4447
import java.util.concurrent.ExecutorService;
@@ -58,6 +61,7 @@ public class BSLWorkspaceService implements WorkspaceService {
5861
private final LanguageServerConfiguration configuration;
5962
private final CommandProvider commandProvider;
6063
private final SymbolProvider symbolProvider;
64+
private final ServerContext serverContext;
6165

6266
private final ExecutorService executorService = Executors.newCachedThreadPool(new CustomizableThreadFactory("workspace-service-"));
6367

@@ -85,7 +89,20 @@ public void didChangeConfiguration(DidChangeConfigurationParams params) {
8589

8690
@Override
8791
public void didChangeWatchedFiles(DidChangeWatchedFilesParams params) {
88-
// no-op
92+
CompletableFuture.runAsync(
93+
() -> {
94+
for (var fileEvent : params.getChanges()) {
95+
var uri = Absolute.uri(fileEvent.getUri());
96+
97+
switch (fileEvent.getType()) {
98+
case Deleted -> handleDeletedFileEvent(uri);
99+
case Created -> handleCreatedFileEvent(uri);
100+
case Changed -> handleChangedFileEvent(uri);
101+
}
102+
}
103+
},
104+
executorService
105+
);
89106
}
90107

91108
@Override
@@ -97,4 +114,70 @@ public CompletableFuture<Object> executeCommand(ExecuteCommandParams params) {
97114
executorService
98115
);
99116
}
117+
118+
/**
119+
* Обрабатывает событие удаления файла из файловой системы.
120+
* <p>
121+
* Если файл был открыт в редакторе, сначала закрывает его и очищает вторичные данные.
122+
* Затем полностью удаляет документ из контекста сервера, включая все связанные метаданные.
123+
*
124+
* @param uri URI удаленного файла
125+
*/
126+
private void handleDeletedFileEvent(URI uri) {
127+
var documentContext = serverContext.getDocument(uri);
128+
if (documentContext == null) {
129+
return;
130+
}
131+
132+
var isDocumentOpened = serverContext.isDocumentOpened(documentContext);
133+
if (isDocumentOpened) {
134+
serverContext.closeDocument(documentContext);
135+
}
136+
serverContext.removeDocument(uri);
137+
}
138+
139+
/**
140+
* Обрабатывает событие создания нового файла в файловой системе.
141+
* <p>
142+
* Добавляет файл в контекст сервера. Если файл не открыт в редакторе,
143+
* выполняет его парсинг и анализ, после чего сразу очищает вторичные данные
144+
* для экономии памяти. Для открытых файлов обработка пропускается,
145+
* т.к. их содержимое управляется через события textDocument/didOpen.
146+
*
147+
* @param uri URI созданного файла
148+
*/
149+
private void handleCreatedFileEvent(URI uri) {
150+
var documentContext = serverContext.addDocument(uri);
151+
152+
var isDocumentOpened = serverContext.isDocumentOpened(documentContext);
153+
if (!isDocumentOpened) {
154+
serverContext.rebuildDocument(documentContext);
155+
serverContext.tryClearDocument(documentContext);
156+
}
157+
}
158+
159+
/**
160+
* Обрабатывает событие изменения файла в файловой системе.
161+
* <p>
162+
* Если файл уже есть в контексте сервера и не открыт в редакторе,
163+
* перечитывает его содержимое с диска, выполняет повторный парсинг и анализ,
164+
* после чего очищает вторичные данные. Для открытых файлов обработка пропускается,
165+
* т.к. их актуальное содержимое управляется через события textDocument/didChange.
166+
* <p>
167+
* Если файл отсутствует в контексте, добавляет его и обрабатывает аналогично созданному.
168+
*
169+
* @param uri URI измененного файла
170+
*/
171+
private void handleChangedFileEvent(URI uri) {
172+
var documentContext = serverContext.getDocument(uri);
173+
if (documentContext == null) {
174+
documentContext = serverContext.addDocument(uri);
175+
}
176+
177+
var isDocumentOpened = serverContext.isDocumentOpened(documentContext);
178+
if (!isDocumentOpened) {
179+
serverContext.rebuildDocument(documentContext);
180+
serverContext.tryClearDocument(documentContext);
181+
}
182+
}
100183
}

0 commit comments

Comments
 (0)