Skip to content

Commit

Permalink
upstream LSP: change virtual file name format (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas authored Aug 26, 2024
1 parent cbc1c63 commit 2309c5c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
32 changes: 21 additions & 11 deletions src/language-server/project/rover/DocumentSynchronization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
findContainedSourceAndPosition,
rangeInContainingDocument,
} from "../../utilities/source";
import { URI } from "vscode-uri";
import { DEBUG } from "./project";

export interface FilePart {
fractionalIndex: string;
Expand Down Expand Up @@ -71,14 +73,21 @@ export function handleFilePartUpdates(
return newParts;
}

function getUri(part: FilePart) {
return part.source.name + "/" + part.fractionalIndex + ".graphql";
function getUri(document: TextDocument, part: FilePart) {
let uri = URI.parse(part.source.name);
if (document.languageId !== "graphql") {
uri = uri.with({ fragment: part.fractionalIndex });
}

return uri.toString();
}

function splitUri(fullUri: DocumentUri) {
const result = /^(.*)\/(\w+)\.graphql/.exec(fullUri);
if (!result) return null;
const [, uri, fractionalIndex] = result;
return { uri, fractionalIndex };
const uri = URI.parse(fullUri);
return {
uri: uri.with({ fragment: null }).toString(),
fractionalIndex: uri.fragment || "a0",
};
}

export class DocumentSynchronization {
Expand Down Expand Up @@ -146,7 +155,7 @@ export class DocumentSynchronization {
if (!previousPart) {
await this.sendNotification(DidOpenTextDocumentNotification.type, {
textDocument: {
uri: getUri(newPart),
uri: getUri(document, newPart),
languageId: "graphql",
version: document.version,
text: newPart.source.body,
Expand All @@ -155,7 +164,7 @@ export class DocumentSynchronization {
} else if (newPart.source.body !== previousPart.source.body) {
await this.sendNotification(DidChangeTextDocumentNotification.type, {
textDocument: {
uri: getUri(newPart),
uri: getUri(document, newPart),
version: document.version,
},
contentChanges: [
Expand All @@ -170,7 +179,7 @@ export class DocumentSynchronization {
if (!newObj[previousPart.fractionalIndex]) {
await this.sendNotification(DidCloseTextDocumentNotification.type, {
textDocument: {
uri: getUri(previousPart),
uri: getUri(document, previousPart),
},
});
}
Expand Down Expand Up @@ -201,7 +210,7 @@ export class DocumentSynchronization {
known.parts.map((part) =>
this.sendNotification(DidCloseTextDocumentNotification.type, {
textDocument: {
uri: getUri(part),
uri: getUri(known.full, part),
},
}),
),
Expand Down Expand Up @@ -246,13 +255,14 @@ export class DocumentSynchronization {
if (!match) return;
return cb({
textDocument: {
uri: getUri(match),
uri: getUri(found.full, match),
},
position: match.position,
});
}

handlePartDiagnostics(params: PublishDiagnosticsParams) {
DEBUG && console.log("Received diagnostics", params);
const uriDetails = splitUri(params.uri);
if (!uriDetails) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/language-server/project/rover/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { DocumentSynchronization } from "./DocumentSynchronization";
import { AsyncLocalStorage } from "node:async_hooks";
import internal from "node:stream";

const DEBUG = true;
export const DEBUG = true;

export function isRoverConfig(config: ApolloConfig): config is RoverConfig {
return config instanceof RoverConfig;
Expand Down

0 comments on commit 2309c5c

Please sign in to comment.