-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguage.ts
83 lines (75 loc) · 2.35 KB
/
language.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const languageExtensions = {
typescript: "ts",
}
/**
* Resolve suffix for a virtual file according to its detected language id.
* If the language id is not supported,
* it will use `txt` (plaintext) as default.
*
* @param languageId LanguageID specified by vscode.
* @returns Suffix of the resolved virtual file.
*/
export function resolveSuffix(languageId: string): string {
return languageId in languageExtensions
? `.${languageExtensions[languageId]}`
: "txt"
}
export type EmbeddedLanguageContent = {
/** Index of the code blocks inside current document. */
index: number
/** Language id of current code block, specified by vscode. */
languageId: string
/**
* Content of the processed code block:
*
* All other contents will be replaced by space with same length
* to mark the same position for the trace in analyze process,
* and the raw code will remain the same.
*/
content: string
}
/**
* Detect the language id of the code inside doc.
*
* 1. If current document is an unsupported language, return undefined.
* 2. If current position is not inside an embedded doc code, return undefined.
* 3. If current position is inside doc code, return the detected content.
* 4. Even if txt, it will return `"plaintext"` as language id.
*
* @param languageId The language id of current document.
* @param content The content of current document.
* @param offset The offset of the position in current document.
* @returns Parsed embedded language content or undefined.
*/
export function detectDocCodeLanguage(
languageId: string,
content: string,
offset: number,
): EmbeddedLanguageContent | undefined {
if (languageId === "markdown") return detectMarkdown(content, offset)
if (languageId === "typescript") return detectTypescript(content, offset)
return undefined
}
function detectMarkdown(
content: string,
offset: number,
): EmbeddedLanguageContent | undefined {
const lines = content.split("\n")
const codeBlocks: [number, number][] = []
let inCodeBlock = false
for (let i = 0; i < lines.length; i++) {
if (lines[i].trimStart().startsWith("```")) {
}
}
return undefined
}
function detectTypescript(
content: string,
offset: number,
): EmbeddedLanguageContent | undefined {
return undefined
}
if (import.meta.vitest) {
const {test} = import.meta.vitest
test("detect typescript", () => {})
}