Skip to content

Commit

Permalink
Fix Document text is not set. at the moment of `workspace/executeCo…
Browse files Browse the repository at this point in the history
…mmand` and `textDocument/documentSymbol`.

closes #34
  • Loading branch information
moetelo committed Jul 24, 2024
1 parent 3f171c7 commit 7420119
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 58 deletions.
12 changes: 2 additions & 10 deletions packages/language-server/src/commands/ExecuteCommandProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ExecuteCommandProvider {
private readonly documentCache: DocumentCache,
) {
connection.onExecuteCommand(
this.onExecuteCommand.bind(this)
this.onExecuteCommand.bind(this),
);
}

Expand All @@ -29,15 +29,7 @@ export class ExecuteCommandProvider {
}

const [uri, position] = params.arguments as [DocumentUri, Position];
const document = this.documentCache.get(uri);

if (!document) {
return;
}

if (!document.text) {
await this.documentCache.setText(document);
}
const document = await this.documentCache.get(uri);

return command(document, position);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class CompletionProvider {
}

async onCompletion(params: CompletionParams) {
const document = this.documentCache.get(params.textDocument.uri);
const document = await this.documentCache.get(params.textDocument.uri);
if (!document) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/language-server/src/definitions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class DefinitionProvider {
async onDefinition(
params: DefinitionParams,
): Promise<Definition | undefined> {
const document = this.documentCache.get(params.textDocument.uri);
const document = await this.documentCache.get(params.textDocument.uri);

if (!document) {
return;
Expand Down
21 changes: 1 addition & 20 deletions packages/language-server/src/documents/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ import { documentUriToFsPath } from '../utils/uri';
import { pointToPosition, rangeContainsPosition } from '../utils/position';
import { getNodeRange } from '../utils/node';

class NoTextError extends Error {
get message() {
return 'Document text is not set. File: ' + documentUriToFsPath(this.uri);
}

constructor(readonly uri: DocumentUri) {
super();
}
}
class TreeNotParsedError extends Error {
get message() {
return 'Document tree is not parsed yet. File: ' + documentUriToFsPath(this.uri);
Expand All @@ -27,7 +18,7 @@ class TreeNotParsedError extends Error {
export class Document {
readonly uri: DocumentUri;

#text: string | null = null;
text: string | null = null;

#tree?: Parser.Tree;
#locals?: LocalSymbolInformation;
Expand Down Expand Up @@ -56,16 +47,6 @@ export class Document {
this.#locals = locals;
}

get text() {
if (this.#text === null) throw new NoTextError(this.uri);

return this.#text;
}

set text(text: string) {
this.#text = text;
}

getBlock(name: string): TwigBlock | undefined {
const symbol = this.locals.block.find((s) => s.name === name);
if (symbol) return symbol;
Expand Down
18 changes: 11 additions & 7 deletions packages/language-server/src/documents/DocumentCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,30 @@ export class DocumentCache {
this.#typeResolver = typeResolver;
}

get(documentUri: DocumentUri) {
async get(documentUri: DocumentUri) {
const document = this.documents.get(documentUri);

if (document) {
if (document.text === null) {
await this.setText(document);
}

return document;
}

return this.add(documentUri);
return await this.add(documentUri);
}

async updateText(documentUri: DocumentUri, text?: string) {
const document = this.get(documentUri);
const document = await this.get(documentUri);

await this.setText(document, text);

return document;
}

async setText(document: Document, text?: string) {
if (text !== undefined) {
if (typeof text === 'string') {
document.text = text;
} else {
const fsPath = documentUriToFsPath(document.uri);
Expand Down Expand Up @@ -75,8 +79,7 @@ export class DocumentCache {

const resolvedTemplate = await resolveTemplate(pathToTwig);
if (resolvedTemplate) {
const newDocument = this.add(toDocumentUri(resolvedTemplate));
await this.setText(newDocument);
const newDocument = await this.add(toDocumentUri(resolvedTemplate));
return newDocument;
}
}
Expand All @@ -100,11 +103,12 @@ export class DocumentCache {
return await this.resolveByTwigPath(twigImport.path)!;
}

private add(documentUri: DocumentUri) {
private async add(documentUri: DocumentUri) {
documentUri = toDocumentUri(documentUri);

const document = new Document(documentUri);
this.documents.set(documentUri, document);
await this.setText(document);
return document;
}
}
3 changes: 1 addition & 2 deletions packages/language-server/src/hovers/HoverProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ export class HoverProvider {
}

async onHover(params: HoverParams) {
const uri = params.textDocument.uri;
const document = this.documentCache.get(uri);
const document = await this.documentCache.get(params.textDocument.uri);

if (!document) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class InlayHintProvider {
const { block, macro, macroArguments } = this.settings;
if (!block && !macro && !macroArguments) return;

const document = this.documentCache.get(params.textDocument.uri);
const document = await this.documentCache.get(params.textDocument.uri);

if (!document) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class ReferenceProvider {
}

async onReferences(params: ReferenceParams) {
const document = this.documentCache.get(params.textDocument.uri);
const document = await this.documentCache.get(params.textDocument.uri);
if (!document) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/language-server/src/references/RenameProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class RenameProvider {
}

async onPrepareRename(params: PrepareRenameParams) {
const document = this.documentCache.get(params.textDocument.uri);
const document = await this.documentCache.get(params.textDocument.uri);
if (!document) {
return;
}
Expand All @@ -33,7 +33,7 @@ export class RenameProvider {
}

async onRenameRequest(params: RenameParams) {
const document = this.documentCache.get(params.textDocument.uri);
const document = await this.documentCache.get(params.textDocument.uri);
if (!document) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export class SemanticTokensProvider {

async serverRequestHandler(params: SemanticTokensParams) {
const semanticTokens: SemanticTokens = { data: [] };
const uri = params.textDocument.uri;
const document = this.documentCache.get(uri);
const document = await this.documentCache.get(params.textDocument.uri);

if (!document) {
return semanticTokens;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class SignatureHelpProvider {
async provideSignatureHelp(
params: SignatureHelpParams,
): Promise<SignatureHelp | undefined> {
const document = this.documentCache.get(params.textDocument.uri);
const document = await this.documentCache.get(params.textDocument.uri);

if (!document) {
return undefined;
Expand Down
11 changes: 1 addition & 10 deletions packages/language-server/src/symbols/SymbolProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,7 @@ export class SymbolProvider {
}

async onDocumentSymbol(params: DocumentSymbolParams): Promise<DocumentSymbol[]> {
const uri = params.textDocument.uri;
const document = this.documentCache.get(uri);

if (!document) {
return [];
}

if (!document.text) {
await this.documentCache.setText(document);
}
const document = await this.documentCache.get(params.textDocument.uri);

return mapLocalsToSymbols(document.locals);
}
Expand Down

0 comments on commit 7420119

Please sign in to comment.