Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow virtual file diagnostics to be reported from the upstream lsp #192

Merged
merged 5 commits into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 44 additions & 22 deletions src/language-server/project/rover/DocumentSynchronization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,17 @@ export class DocumentSynchronization {
private pendingDocumentChanges = new Map<DocumentUri, TextDocument>();
private knownFiles = new Map<
DocumentUri,
{
full: TextDocument;
parts: ReadonlyArray<FilePart>;
}
| {
source: "editor";
full: TextDocument;
parts: ReadonlyArray<FilePart>;
}
| {
source: "lsp";
full: Pick<TextDocument, "uri">;
parts?: undefined;
diagnostics?: Diagnostic[];
}
>();

constructor(
Expand Down Expand Up @@ -158,7 +165,11 @@ export class DocumentSynchronization {
const newObj = Object.fromEntries(
newParts.map((p) => [p.fractionalIndex, p]),
);
this.knownFiles.set(document.uri, { full: document, parts: newParts });
this.knownFiles.set(document.uri, {
source: "editor",
full: document,
parts: newParts,
});

for (const newPart of newParts) {
const previousPart = previousObj[newPart.fractionalIndex];
Expand Down Expand Up @@ -198,7 +209,9 @@ export class DocumentSynchronization {

async resendAllDocuments() {
for (const file of this.knownFiles.values()) {
await this.sendDocumentChanges(file.full, []);
if (file.source === "editor") {
await this.sendDocumentChanges(file.full, []);
}
}
}

Expand All @@ -208,23 +221,23 @@ export class DocumentSynchronization {
this.documentDidChange(params.document);
};

onDidCloseTextDocument: NonNullable<GraphQLProject["onDidClose"]> = (
onDidCloseTextDocument: NonNullable<GraphQLProject["onDidClose"]> = async (
params,
) => {
const known = this.knownFiles.get(params.document.uri);
if (!known) {
return;
}
this.knownFiles.delete(params.document.uri);
return Promise.all(
known.parts.map((part) =>
this.sendNotification(DidCloseTextDocumentNotification.type, {
if (known.source === "editor") {
for (const part of known.parts) {
await this.sendNotification(DidCloseTextDocumentNotification.type, {
textDocument: {
uri: getUri(known.full, part),
},
}),
),
);
});
}
}
};

async documentDidChange(document: TextDocument) {
Expand Down Expand Up @@ -254,7 +267,7 @@ export class DocumentSynchronization {
): Promise<T | undefined> {
await this.synchronizedWithDocument(positionParams.textDocument.uri);
const found = this.knownFiles.get(positionParams.textDocument.uri);
if (!found) {
if (!found || found.source !== "editor") {
return;
}
const match = findContainedSourceAndPosition(
Expand All @@ -274,11 +287,14 @@ export class DocumentSynchronization {
handlePartDiagnostics(params: PublishDiagnosticsParams) {
DEBUG && console.log("Received diagnostics", params);
const uriDetails = splitUri(params.uri);
if (!uriDetails) {
return;
}
const found = this.knownFiles.get(uriDetails.uri);
if (!found) {
if (!found || found.source === "lsp") {
this.knownFiles.set(uriDetails.uri, {
source: "lsp",
full: { uri: uriDetails.uri },
diagnostics: params.diagnostics,
});
this.sendDiagnostics(params);
return;
}
const part = found.parts.find(
Expand All @@ -304,13 +320,19 @@ export class DocumentSynchronization {
}

get openDocuments() {
return [...this.knownFiles.values()].map((f) => f.full);
return [...this.knownFiles.values()]
.filter((f) => f.source === "editor")
.map((f) => f.full);
}

clearAllDiagnostics() {
for (const file of this.knownFiles.values()) {
for (const part of file.parts) {
part.diagnostics = [];
if (file.source === "editor") {
for (const part of file.parts) {
part.diagnostics = [];
}
} else {
file.diagnostics = [];
}
this.sendDiagnostics({ uri: file.full.uri, diagnostics: [] });
}
Expand All @@ -332,7 +354,7 @@ export class DocumentSynchronization {
): Promise<SemanticTokens | null> {
await this.synchronizedWithDocument(params.textDocument.uri);
const found = this.knownFiles.get(params.textDocument.uri);
if (!found) {
if (!found || found.source !== "editor") {
return null;
}
const allParts = await Promise.all(
Expand Down