Skip to content

Commit

Permalink
Вынес логику работы с DocumentState в публичный интерфейс, рефакторин…
Browse files Browse the repository at this point in the history
…г референс файндера оскрипта.
  • Loading branch information
nixel2007 committed Dec 26, 2023
1 parent fee008e commit fdff4f1
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github._1c_syntax.bsl.languageserver.context;

/**
* Состояние документа в контексте.
*/
public enum DocumentState {
/**
* В документе отсутствует контент или он был очищен.
*/
WITHOUT_CONTENT,
/**
* В документе присутствует контент.
*/
WITH_CONTENT
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class ServerContext {
= Collections.synchronizedMap(new HashMap<>());
private final ReadWriteLock contextLock = new ReentrantReadWriteLock();

private final Map<DocumentContext, State> states = new ConcurrentHashMap<>();
private final Map<DocumentContext, DocumentState> states = new ConcurrentHashMap<>();
private final Set<DocumentContext> openedDocuments = ConcurrentHashMap.newKeySet();

public void populateContext() {
Expand Down Expand Up @@ -214,12 +214,12 @@ public void openDocument(DocumentContext documentContext, String content, Intege
* @param documentContext документ, который необходимо перестроить.
*/
public void rebuildDocument(DocumentContext documentContext) {
if (states.get(documentContext) == State.WITH_CONTENT) {
if (states.get(documentContext) == DocumentState.WITH_CONTENT) {
return;
}

documentContext.rebuild();
states.put(documentContext, State.WITH_CONTENT);
states.put(documentContext, DocumentState.WITH_CONTENT);
}

/**
Expand All @@ -231,7 +231,11 @@ public void rebuildDocument(DocumentContext documentContext) {
*/
public void rebuildDocument(DocumentContext documentContext, String content, Integer version) {
documentContext.rebuild(content, version);
states.put(documentContext, State.WITH_CONTENT);
states.put(documentContext, DocumentState.WITH_CONTENT);
}

public DocumentState getDocumentState(DocumentContext documentContext) {
return states.get(documentContext);
}

/**
Expand All @@ -244,7 +248,7 @@ public void tryClearDocument(DocumentContext documentContext) {
return;
}

states.put(documentContext, State.WITHOUT_CONTENT);
states.put(documentContext, DocumentState.WITHOUT_CONTENT);
documentContext.clearSecondaryData();
}

Expand All @@ -255,7 +259,7 @@ public void tryClearDocument(DocumentContext documentContext) {
*/
public void closeDocument(DocumentContext documentContext) {
openedDocuments.remove(documentContext);
states.put(documentContext, State.WITHOUT_CONTENT);
states.put(documentContext, DocumentState.WITHOUT_CONTENT);
documentContext.clearSecondaryData();
}

Expand Down Expand Up @@ -332,18 +336,4 @@ private String getMessage(String key) {
return Resources.getResourceString(languageServerConfiguration.getLanguage(), getClass(), key);
}

/**
* Состояние документа в контексте.
*/
private enum State {
/**
* В документе отсутствует контент или он был очищен.
*/
WITHOUT_CONTENT,
/**
* В документе присутствует контент.
*/
WITH_CONTENT
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.github._1c_syntax.bsl.languageserver.context.symbol.variable.VariableKind;
import com.github._1c_syntax.bsl.languageserver.utils.Ranges;
import com.github._1c_syntax.bsl.languageserver.utils.Trees;
import com.github._1c_syntax.bsl.languageserver.utils.bsl.Constructors;
import com.github._1c_syntax.bsl.parser.BSLParser;
import com.github._1c_syntax.bsl.parser.BSLParserBaseVisitor;
import com.github._1c_syntax.bsl.parser.BSLParserRuleContext;
Expand Down Expand Up @@ -282,11 +283,8 @@ private void updateVariablesCache(TerminalNode node, Optional<VariableDescriptio
private String typeName(BSLParser.AssignmentContext ctx) {
var typeName = "";
var newCtx = Trees.getNextNode(ctx.expression(), ctx.expression(), BSLParser.RULE_newExpression);
if (newCtx instanceof BSLParser.NewExpressionContext) {
var typeNamectx = ((BSLParser.NewExpressionContext) newCtx).typeName();
if (typeNamectx != null) {
typeName = typeNamectx.getText();
}
if (newCtx instanceof BSLParser.NewExpressionContext newExpression) {
typeName = Constructors.typeName(newExpression).orElse("");
}
return typeName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
Expand All @@ -50,6 +49,6 @@ public List<Location> getReferences(DocumentContext documentContext, ReferencePa
.map(referenceIndex::getReferencesTo)
.flatMap(Collection::stream)
.map(Reference::toLocation)
.collect(Collectors.toList());
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
*/
package com.github._1c_syntax.bsl.languageserver.references;

import com.github._1c_syntax.bsl.languageserver.context.DocumentContext;
import com.github._1c_syntax.bsl.languageserver.context.DocumentState;
import com.github._1c_syntax.bsl.languageserver.context.ServerContext;
import com.github._1c_syntax.bsl.languageserver.providers.SelectionRangeProvider;
import com.github._1c_syntax.bsl.languageserver.references.model.OccurrenceType;
import com.github._1c_syntax.bsl.languageserver.references.model.Reference;
import com.github._1c_syntax.bsl.languageserver.utils.Ranges;
import com.github._1c_syntax.bsl.languageserver.utils.Trees;
import com.github._1c_syntax.bsl.parser.BSLParser;
import com.github._1c_syntax.bsl.types.ModuleType;
import lombok.RequiredArgsConstructor;
Expand All @@ -37,7 +37,6 @@
import java.net.URI;
import java.util.Optional;


@Component
@RequiredArgsConstructor
public class OscriptReferenceFinder implements ReferenceFinder {
Expand All @@ -47,25 +46,24 @@ public class OscriptReferenceFinder implements ReferenceFinder {
@Override
public Optional<Reference> findReference(URI uri, Position position) {

DocumentContext document = serverContext.getDocument(uri);
var document = serverContext.getDocument(uri);
if (document == null) {
return Optional.empty();
}

if(document.isComputedDataFrozen()) {
document.unfreezeComputedData();
serverContext.rebuildDocument(document);
var documentState = serverContext.getDocumentState(document);
if (documentState == DocumentState.WITHOUT_CONTENT) {
return Optional.empty();
}

var node = SelectionRangeProvider.findNodeContainsPosition(document.getAst(), position);
var node = Trees.findTerminalNodeContainsPosition(document.getAst(), position);

if (node.isEmpty()) {
return Optional.empty();
}

return serverContext.getDocuments().values().stream()
.filter(documentContext -> documentContext.getTypeName().equals(node.get().getText()))
.filter(d -> filterByType(node, d.getModuleType()))
.filter(documentContext -> filterByType(node, documentContext.getModuleType()))
.map(documentContext -> new Reference(
documentContext.getSymbolTree().getModule(),
documentContext.getSymbolTree().getModule(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
package com.github._1c_syntax.bsl.languageserver.providers;

import com.github._1c_syntax.bsl.languageserver.context.ServerContext;
import com.github._1c_syntax.bsl.languageserver.util.CleanupContextBeforeClassAndAfterClass;
import com.github._1c_syntax.bsl.languageserver.util.CleanupContextBeforeClassAndAfterEachTestMethod;
import com.github._1c_syntax.bsl.languageserver.util.TestUtils;
import com.github._1c_syntax.bsl.languageserver.utils.Ranges;
import com.github._1c_syntax.bsl.types.ModuleType;
import jakarta.annotation.PostConstruct;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.ReferenceParams;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -40,7 +40,7 @@
import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
@CleanupContextBeforeClassAndAfterClass
@CleanupContextBeforeClassAndAfterEachTestMethod
class ReferencesProviderTest {

@Autowired
Expand All @@ -51,7 +51,7 @@ class ReferencesProviderTest {

private static final String PATH_TO_FILE = "./src/test/resources/providers/references.bsl";

@PostConstruct
@BeforeEach
void prepareServerContext() {
serverContext.setConfigurationRoot(Paths.get(PATH_TO_METADATA));
serverContext.populateContext();
Expand Down

0 comments on commit fdff4f1

Please sign in to comment.