Skip to content

Commit

Permalink
fix: correct reparse also when editing .profile documents
Browse files Browse the repository at this point in the history
  • Loading branch information
Mangern committed Nov 1, 2024
1 parent 092ae74 commit 9f747ef
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public SearchResult(NodeType node, ResultType result) {

public InheritanceGraph() { }

// Note: does not remove childrenOfNode
// because if this node is added back we
// lazily remember the children, and access valid children through
// getValidChildren
public void clearInheritsList(NodeType node) {
if (!nodeExists(node)) return;

Expand All @@ -45,7 +49,7 @@ public void clearInheritsList(NodeType node) {
}

parentsOfNode.remove(node);
childrenOfNode.remove(node);

}

public void createNodeIfNotExists(NodeType node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,18 @@ public void insertSymbolReference(Symbol definition, Symbol reference) {

definitionOfReference.put(reference, definition);
list.add(reference);

if (reference.getType() == SymbolType.RANK_PROFILE && reference.getScope() != null) {
tryRegisterRankProfileInheritance(reference.getScope(), definition);
}

if (reference.getType() == SymbolType.STRUCT && reference.getScope() != null) {
tryRegisterStructInheritance(reference.getScope(), definition);
}

if (reference.getType() == SymbolType.DOCUMENT_SUMMARY && reference.getScope() != null) {
tryRegisterDocumentSummaryInheritance(reference.getScope(), definition);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -91,49 +93,24 @@ public void updateFile(String fileURI, String content, Integer version) {

// TODO: a lot of parsing going on. It mostly should be reference resolving, not necessarily reparsing of entire contents.
workspaceFiles.get(fileURI).updateFileContent(content, version);
boolean needsReparse = false;

if (documentType.get() == DocumentType.SCHEMA && reparseDescendants) {
if (reparseDescendants) {
Set<String> parsedURIs = new HashSet<>() {{ add(fileURI); }};
for (String descendantURI : schemaIndex.getDocumentInheritanceGraph().getAllDescendants(fileURI)) {
if (descendantURI.equals(fileURI)) continue;
if (documentType.get() == DocumentType.SCHEMA) {

if (workspaceFiles.containsKey(descendantURI)) {
workspaceFiles.get(descendantURI).reparseContent();
parsedURIs.add(descendantURI);
}
}
reparseSchemaFileDescendants(fileURI, parsedURIs);

// Reparse documents that holds references to this document
String schemaIdentifier = ((SchemaDocument)workspaceFiles.get(fileURI)).getSchemaIdentifier();
Optional<Symbol> documentDefinition = schemaIndex.findSymbol(null, SymbolType.DOCUMENT, schemaIdentifier);

if (documentDefinition.isPresent()) {
for (Symbol referencesThisDocument : schemaIndex.getDocumentReferenceGraph().getAllDescendants(documentDefinition.get())) {
String descendantURI = referencesThisDocument.getFileURI();
if (!parsedURIs.contains(descendantURI) && workspaceFiles.containsKey(descendantURI)) {
workspaceFiles.get(referencesThisDocument.getFileURI()).reparseContent();
parsedURIs.add(descendantURI);
}
}
}
} else if (documentType.get() == DocumentType.PROFILE) {
// Find the schema this rank profile belongs to and reparse from there
RankProfileDocument document = getRankProfileDocument(fileURI);
Optional<Symbol> schemaSymbol = document.schemaSymbol();

// reparse rank profile files belonging to this document
for (var entry : workspaceFiles.entrySet()) {
if ((entry.getValue() instanceof RankProfileDocument)) {
RankProfileDocument document = (RankProfileDocument)entry.getValue();
if (document.schemaSymbol().isPresent() && document.schemaSymbol().get().fileURIEquals(fileURI)) {
entry.getValue().reparseContent();
needsReparse = true;
}
if (schemaSymbol.isPresent()) {
reparseSchemaFileDescendants(schemaSymbol.get().getFileURI(), parsedURIs);
}
}
}

if (needsReparse) {
workspaceFiles.get(fileURI).reparseContent();
}

// Resolve remaining unresolved symbols after everything has parsed
Map<String, List<Diagnostic>> undefinedSymbolDiagnostics = new HashMap<>();
while (true) {
Expand Down Expand Up @@ -174,6 +151,41 @@ public void updateFile(String fileURI, String content, Integer version) {
}
}

private void reparseSchemaFileDescendants(String fileURI, Set<String> parsedURIs) {
for (String descendantURI : schemaIndex.getDocumentInheritanceGraph().getAllDescendants(fileURI)) {
if (parsedURIs.contains(descendantURI)) continue;
if (workspaceFiles.containsKey(descendantURI)) {
workspaceFiles.get(descendantURI).reparseContent();
parsedURIs.add(descendantURI);
}
}

// Reparse documents that holds references to this document
String schemaIdentifier = ((SchemaDocument)workspaceFiles.get(fileURI)).getSchemaIdentifier();
Optional<Symbol> documentDefinition = schemaIndex.findSymbol(null, SymbolType.DOCUMENT, schemaIdentifier);

if (documentDefinition.isPresent()) {
for (Symbol referencesThisDocument : schemaIndex.getDocumentReferenceGraph().getAllDescendants(documentDefinition.get())) {
String descendantURI = referencesThisDocument.getFileURI();
if (!parsedURIs.contains(descendantURI) && workspaceFiles.containsKey(descendantURI)) {
workspaceFiles.get(referencesThisDocument.getFileURI()).reparseContent();
parsedURIs.add(descendantURI);
}
}
}
// reparse rank profile files belonging to this document
for (var entry : workspaceFiles.entrySet()) {
if (parsedURIs.contains(entry.getKey())) continue;
if ((entry.getValue() instanceof RankProfileDocument)) {
RankProfileDocument document = (RankProfileDocument)entry.getValue();
if (document.schemaSymbol().isPresent() && document.schemaSymbol().get().fileURIEquals(fileURI)) {
entry.getValue().reparseContent();
parsedURIs.add(entry.getKey());
}
}
}
}

public String getWorkspaceURI() {
if (this.workspaceURI == null) return null;
return this.workspaceURI.toString();
Expand Down Expand Up @@ -227,13 +239,18 @@ public void closeDocument(String fileURI) {
if (!file.exists()) {
cleanUpDocument(fileURI);
}

if (!isInWorkspace(fileURI)) {
diagnosticsHandler.clearDiagnostics(fileURI);
}
}

private void cleanUpDocument(String fileURI) {
logger.info("Removing document: "+ fileURI);

schemaIndex.clearDocument(fileURI);
workspaceFiles.remove(fileURI);
diagnosticsHandler.clearDiagnostics(fileURI);
}

public boolean removeDocument(String fileURI) {
Expand Down Expand Up @@ -300,4 +317,11 @@ public void setupWorkspace(URI workspaceURI) {
reparseInInheritanceOrder();
setReparseDescendants(true);
}

private boolean isInWorkspace(String fileURI) {
if (workspaceURI == null) return false;
Path filePath = Paths.get(URI.create(fileURI));
Path workspacePath = Paths.get(workspaceURI);
return filePath.startsWith(workspacePath);
}
}

0 comments on commit 9f747ef

Please sign in to comment.