Skip to content

Commit 2f64bc4

Browse files
committed
Кривая реализация анализа проекта при открытии
1 parent 020ad31 commit 2f64bc4

File tree

8 files changed

+155
-2
lines changed

8 files changed

+155
-2
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* This file is a part of BSL Language Server.
3+
*
4+
* Copyright (c) 2018-2022
5+
* Alexey Sosnoviy <labotamy@gmail.com>, Nikita Fedkin <nixel2007@gmail.com> and contributors
6+
*
7+
* SPDX-License-Identifier: LGPL-3.0-or-later
8+
*
9+
* BSL Language Server is free software; you can redistribute it and/or
10+
* modify it under the terms of the GNU Lesser General Public
11+
* License as published by the Free Software Foundation; either
12+
* version 3.0 of the License, or (at your option) any later version.
13+
*
14+
* BSL Language Server is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
* Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public
20+
* License along with BSL Language Server.
21+
*/
22+
package com.github._1c_syntax.bsl.languageserver;
23+
24+
import com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration;
25+
import com.github._1c_syntax.bsl.languageserver.context.DocumentContext;
26+
import com.github._1c_syntax.bsl.languageserver.context.events.ServerContextPopulatedEvent;
27+
import com.github._1c_syntax.bsl.languageserver.providers.DiagnosticProvider;
28+
import lombok.RequiredArgsConstructor;
29+
import org.springframework.context.event.EventListener;
30+
import org.springframework.stereotype.Component;
31+
32+
@Component
33+
@RequiredArgsConstructor
34+
public class AnalyzeProjectOnStart {
35+
36+
private final LanguageServerConfiguration configuration;
37+
private final DiagnosticProvider diagnosticProvider;
38+
private final WorkDoneProgressHelper workDoneProgressHelper;
39+
40+
@EventListener
41+
public void handleEvent(ServerContextPopulatedEvent event) {
42+
if (!configuration.getDiagnosticsOptions().isAnalyzeOnStart()) {
43+
return;
44+
}
45+
46+
var serverContext = event.getSource();
47+
48+
var documentContexts = serverContext.getDocuments().values();
49+
var progress = workDoneProgressHelper.createProgress(documentContexts.size(), " files");
50+
progress.beginProgress("Analyzing project");
51+
52+
documentContexts.forEach((DocumentContext documentContext) -> {
53+
54+
progress.tick();
55+
56+
var withContent = documentContext.isWithContent();
57+
if (!withContent) {
58+
documentContext.rebuild();
59+
}
60+
diagnosticProvider.computeAndPublishDiagnostics(documentContext);
61+
if (!withContent) {
62+
documentContext.clearSecondaryData();
63+
}
64+
65+
});
66+
67+
progress.endProgress("Project analyzed");
68+
}
69+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@
9494

9595
import java.util.Collections;
9696
import java.util.List;
97+
import java.util.Set;
9798
import java.util.concurrent.CompletableFuture;
99+
import java.util.concurrent.ConcurrentHashMap;
98100
import java.util.stream.Collectors;
99101

100102
@Component
@@ -118,6 +120,8 @@ public class BSLTextDocumentService implements TextDocumentService, ProtocolExte
118120
private final ColorProvider colorProvider;
119121
private final RenameProvider renameProvider;
120122

123+
private final Set<DocumentContext> openedDocuments = ConcurrentHashMap.newKeySet();
124+
121125
@Override
122126
public CompletableFuture<Hover> hover(HoverParams params) {
123127
var documentContext = context.getDocument(params.getTextDocument().getUri());
@@ -309,6 +313,7 @@ public void didOpen(DidOpenTextDocumentParams params) {
309313
if (configuration.getDiagnosticsOptions().getComputeTrigger() != ComputeTrigger.NEVER) {
310314
validate(documentContext);
311315
}
316+
openedDocuments.add(documentContext);
312317
}
313318

314319
@Override
@@ -333,6 +338,7 @@ public void didClose(DidCloseTextDocumentParams params) {
333338
if (documentContext == null) {
334339
return;
335340
}
341+
openedDocuments.remove(documentContext);
336342

337343
documentContext.clearSecondaryData();
338344

src/main/java/com/github/_1c_syntax/bsl/languageserver/aop/EventPublisherAspect.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration;
2525
import com.github._1c_syntax.bsl.languageserver.configuration.events.LanguageServerConfigurationChangedEvent;
2626
import com.github._1c_syntax.bsl.languageserver.context.DocumentContext;
27+
import com.github._1c_syntax.bsl.languageserver.context.ServerContext;
2728
import com.github._1c_syntax.bsl.languageserver.context.events.DocumentContextContentChangedEvent;
29+
import com.github._1c_syntax.bsl.languageserver.context.events.ServerContextPopulatedEvent;
2830
import com.github._1c_syntax.bsl.languageserver.events.LanguageServerInitializeRequestReceivedEvent;
2931
import lombok.NoArgsConstructor;
3032
import lombok.extern.slf4j.Slf4j;
@@ -38,6 +40,8 @@
3840
import org.springframework.context.ApplicationEventPublisherAware;
3941

4042
import javax.annotation.PreDestroy;
43+
import java.io.File;
44+
import java.util.Collection;
4145

4246
/**
4347
* Аспект подсистемы событий.
@@ -74,6 +78,11 @@ public void documentContextRebuild(JoinPoint joinPoint) {
7478
publishEvent(new DocumentContextContentChangedEvent((DocumentContext) joinPoint.getThis()));
7579
}
7680

81+
@AfterReturning("Pointcuts.isServerContext() && Pointcuts.isPopulateContextCall() && args(files)")
82+
public void serverContextPopulated(JoinPoint joinPoint, Collection<File> files) {
83+
publishEvent(new ServerContextPopulatedEvent((ServerContext) joinPoint.getThis()));
84+
}
85+
7786
@AfterReturning("Pointcuts.isLanguageServer() && Pointcuts.isInitializeCall() && args(initializeParams)")
7887
public void languageServerInitialize(JoinPoint joinPoint, InitializeParams initializeParams) {
7988
var event = new LanguageServerInitializeRequestReceivedEvent(

src/main/java/com/github/_1c_syntax/bsl/languageserver/aop/Pointcuts.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ public void isRebuildCall() {
107107
// no-op
108108
}
109109

110+
/**
111+
* Это вызов метода populateContext.
112+
*/
113+
@Pointcut("isBSLLanguageServerScope() && execution(* populateContext(..))")
114+
public void isPopulateContextCall() {
115+
// no-op
116+
}
117+
110118
/**
111119
* Это вызов метода update.
112120
*/

src/main/java/com/github/_1c_syntax/bsl/languageserver/configuration/diagnostics/DiagnosticsOptions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
@JsonIgnoreProperties(ignoreUnknown = true)
4343
public class DiagnosticsOptions {
4444
private ComputeTrigger computeTrigger = ComputeTrigger.ONSAVE;
45+
private boolean analyzeOnStart;
4546
private SkipSupport skipSupport = SkipSupport.NEVER;
4647
private Mode mode = Mode.ON;
4748
private boolean ordinaryAppSupport = true;

src/main/java/com/github/_1c_syntax/bsl/languageserver/context/DocumentContext.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747
import lombok.Getter;
4848
import lombok.RequiredArgsConstructor;
4949
import lombok.Setter;
50+
import lombok.extern.slf4j.Slf4j;
5051
import org.antlr.v4.runtime.Token;
52+
import org.apache.commons.io.FileUtils;
5153
import org.apache.commons.io.FilenameUtils;
5254
import org.eclipse.lsp4j.Diagnostic;
5355
import org.eclipse.lsp4j.Position;
@@ -59,7 +61,10 @@
5961

6062
import javax.annotation.Nullable;
6163
import javax.annotation.PostConstruct;
64+
import java.io.File;
65+
import java.io.IOException;
6266
import java.net.URI;
67+
import java.nio.charset.StandardCharsets;
6368
import java.util.Collections;
6469
import java.util.List;
6570
import java.util.Locale;
@@ -75,6 +80,7 @@
7580
@Component
7681
@Scope("prototype")
7782
@RequiredArgsConstructor
83+
@Slf4j
7884
public class DocumentContext {
7985

8086
private static final Pattern CONTENT_SPLIT_PATTERN = Pattern.compile("\r?\n|\r");
@@ -87,6 +93,9 @@ public class DocumentContext {
8793
@Getter
8894
private int version;
8995

96+
@Getter
97+
private boolean withContent;
98+
9099
@Setter(onMethod = @__({@Autowired}))
91100
private ServerContext context;
92101
@Setter(onMethod = @__({@Autowired}))
@@ -269,9 +278,8 @@ public void rebuild(String content, int version) {
269278
computeLock.lock();
270279

271280
boolean versionMatches = version == this.version && version != 0;
272-
boolean contentWasCleared = this.content == null;
273281

274-
if (versionMatches && !contentWasCleared) {
282+
if (versionMatches && withContent) {
275283
clearDependantData();
276284
computeLock.unlock();
277285
return;
@@ -285,13 +293,24 @@ public void rebuild(String content, int version) {
285293
tokenizer = new BSLTokenizer(content);
286294
this.version = version;
287295
symbolTree = computeSymbolTree();
296+
withContent = true;
288297

289298
computeLock.unlock();
290299
}
291300

301+
public void rebuild() {
302+
try {
303+
var newContent = FileUtils.readFileToString(new File(uri), StandardCharsets.UTF_8);
304+
rebuild(newContent, 0);
305+
} catch (IOException e) {
306+
LOGGER.error("Can't rebuild content from uri", e);
307+
}
308+
}
309+
292310
public void clearSecondaryData() {
293311
computeLock.lock();
294312

313+
withContent = false;
295314
content = null;
296315
contentList.clear();
297316
tokenizer = null;

src/main/java/com/github/_1c_syntax/bsl/languageserver/context/ServerContext.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import com.github._1c_syntax.bsl.languageserver.WorkDoneProgressHelper;
2525
import com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration;
26+
import com.github._1c_syntax.bsl.languageserver.providers.DiagnosticProvider;
2627
import com.github._1c_syntax.bsl.languageserver.utils.MdoRefBuilder;
2728
import com.github._1c_syntax.bsl.languageserver.utils.Resources;
2829
import com.github._1c_syntax.bsl.types.ModuleType;
@@ -61,6 +62,7 @@ public class ServerContext {
6162
private final ObjectProvider<DocumentContext> documentContextProvider;
6263
private final WorkDoneProgressHelper workDoneProgressHelper;
6364
private final LanguageServerConfiguration languageServerConfiguration;
65+
private final DiagnosticProvider diagnosticProvider;
6466

6567
private final Map<URI, DocumentContext> documents = Collections.synchronizedMap(new HashMap<>());
6668
private final Lazy<Configuration> configurationMetadata = new Lazy<>(this::computeConfigurationMetadata);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This file is a part of BSL Language Server.
3+
*
4+
* Copyright (c) 2018-2022
5+
* Alexey Sosnoviy <labotamy@gmail.com>, Nikita Fedkin <nixel2007@gmail.com> and contributors
6+
*
7+
* SPDX-License-Identifier: LGPL-3.0-or-later
8+
*
9+
* BSL Language Server is free software; you can redistribute it and/or
10+
* modify it under the terms of the GNU Lesser General Public
11+
* License as published by the Free Software Foundation; either
12+
* version 3.0 of the License, or (at your option) any later version.
13+
*
14+
* BSL Language Server is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
* Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public
20+
* License along with BSL Language Server.
21+
*/
22+
package com.github._1c_syntax.bsl.languageserver.context.events;
23+
24+
import com.github._1c_syntax.bsl.languageserver.context.ServerContext;
25+
import org.springframework.context.ApplicationEvent;
26+
27+
public class ServerContextPopulatedEvent extends ApplicationEvent {
28+
29+
private static final long serialVersionUID = -4485675935728156708L;
30+
31+
public ServerContextPopulatedEvent(ServerContext source) {
32+
super(source);
33+
}
34+
35+
@Override
36+
public ServerContext getSource() {
37+
return (ServerContext) super.getSource();
38+
}
39+
}

0 commit comments

Comments
 (0)