|
1 | 1 | import * as lsp from "vscode-languageserver" |
2 | 2 | import {TextDocument} from "vscode-languageserver-textdocument" |
3 | 3 | import {pathToFileURL} from "node:url" |
4 | | -import {createFiftParser, createTlbParser, createTolkParser} from "@server/parser" |
| 4 | +import {createFiftParser, createFuncParser, createTlbParser, createTolkParser} from "@server/parser" |
5 | 5 | import {readFileVFS, globalVFS} from "@server/vfs/files-adapter" |
6 | 6 | import {FiftFile} from "@server/languages/fift/psi/FiftFile" |
7 | 7 | import {TlbFile} from "@server/languages/tlb/psi/TlbFile" |
8 | 8 | import {URI} from "vscode-uri" |
9 | 9 | import {TolkFile} from "@server/languages/tolk/psi/TolkFile" |
10 | 10 | import {measureTime} from "@server/psi/utils" |
| 11 | +import {FuncFile} from "@server/languages/func/psi/FuncFile" |
11 | 12 |
|
12 | 13 | export const TOLK_PARSED_FILES_CACHE: Map<string, TolkFile> = new Map() |
| 14 | +export const FUNC_PARSED_FILES_CACHE: Map<string, FuncFile> = new Map() |
13 | 15 | export const FIFT_PARSED_FILES_CACHE: Map<string, FiftFile> = new Map() |
14 | 16 | export const TLB_PARSED_FILES_CACHE: Map<string, TlbFile> = new Map() |
15 | 17 |
|
@@ -41,6 +43,34 @@ export function reparseTolkFile(uri: string, content: string): TolkFile { |
41 | 43 | return file |
42 | 44 | } |
43 | 45 |
|
| 46 | +export async function findFuncFile(uri: string, changed: boolean = false): Promise<FuncFile> { |
| 47 | + const cached = FUNC_PARSED_FILES_CACHE.get(uri) |
| 48 | + if (cached !== undefined && !changed) { |
| 49 | + return cached |
| 50 | + } |
| 51 | + |
| 52 | + const rawContent = await readOrUndefined(uri) |
| 53 | + if (rawContent === undefined) { |
| 54 | + console.error(`cannot read ${uri} file`) |
| 55 | + } |
| 56 | + |
| 57 | + const content = rawContent ?? "" |
| 58 | + return measureTime(`reparse ${uri}`, () => reparseFuncFile(uri, content)) |
| 59 | +} |
| 60 | + |
| 61 | +export function reparseFuncFile(uri: string, content: string): FuncFile { |
| 62 | + const parser = createFuncParser() |
| 63 | + const tree = parser.parse(content) |
| 64 | + if (!tree) { |
| 65 | + throw new Error(`FATAL ERROR: cannot parse ${uri} file`) |
| 66 | + } |
| 67 | + |
| 68 | + // TODO: why we have %40 here? |
| 69 | + const file = new FuncFile(uri.replace("%40", "@"), tree, content) |
| 70 | + FUNC_PARSED_FILES_CACHE.set(uri, file) |
| 71 | + return file |
| 72 | +} |
| 73 | + |
44 | 74 | export async function findFiftFile(uri: string): Promise<FiftFile> { |
45 | 75 | const cached = FIFT_PARSED_FILES_CACHE.get(uri) |
46 | 76 | if (cached !== undefined) { |
@@ -108,6 +138,11 @@ export const isTolkFile = ( |
108 | 138 | event?: lsp.TextDocumentChangeEvent<TextDocument>, |
109 | 139 | ): boolean => event?.document.languageId === "tolk" || uri.endsWith(".tolk") |
110 | 140 |
|
| 141 | +export const isFuncFile = ( |
| 142 | + uri: string, |
| 143 | + event?: lsp.TextDocumentChangeEvent<TextDocument>, |
| 144 | +): boolean => event?.document.languageId === "func" || uri.endsWith(".fc") || uri.endsWith(".func") |
| 145 | + |
111 | 146 | export const isFiftFile = ( |
112 | 147 | uri: string, |
113 | 148 | event?: lsp.TextDocumentChangeEvent<TextDocument>, |
|
0 commit comments