|
| 1 | +import { |
| 2 | + ScopeSupportFacetLevel, |
| 3 | + getScopeTestPathsRecursively, |
| 4 | + getScopeTestsDirPath, |
| 5 | + groupBy, |
| 6 | + languageScopeSupport, |
| 7 | + scopeSupportFacetInfos, |
| 8 | + showInfo, |
| 9 | + type IDE, |
| 10 | + type ScopeSupportFacet, |
| 11 | +} from "@cursorless/common"; |
| 12 | +import * as fs from "node:fs"; |
| 13 | +import * as fsPromises from "node:fs/promises"; |
| 14 | +import * as path from "node:path"; |
| 15 | + |
| 16 | +export class ScopeTestRecorder { |
| 17 | + constructor(private ide: IDE) { |
| 18 | + this.showUnimplementedFacets = this.showUnimplementedFacets.bind(this); |
| 19 | + this.saveActiveDocument = this.saveActiveDocument.bind(this); |
| 20 | + } |
| 21 | + |
| 22 | + async showUnimplementedFacets() { |
| 23 | + const languageId = await this.languageSelection(); |
| 24 | + |
| 25 | + if (languageId == null) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + const supportedScopeFacets = getSupportedScopeFacets(languageId); |
| 30 | + const existingScopeTestFacets = getExistingScopeFacetTest(languageId); |
| 31 | + |
| 32 | + const missingScopeFacets = supportedScopeFacets.filter( |
| 33 | + (facet) => !existingScopeTestFacets.has(facet), |
| 34 | + ); |
| 35 | + |
| 36 | + let currentSnippetPlaceholder = 1; |
| 37 | + const missingScopeFacetRows = missingScopeFacets.map( |
| 38 | + (facet) => |
| 39 | + `[${facet}] - ${scopeSupportFacetInfos[facet].description}\n$${currentSnippetPlaceholder++}\n---\n`, |
| 40 | + ); |
| 41 | + const header = `[[${languageId}]]\n\n`; |
| 42 | + const snippetText = `${header}${missingScopeFacetRows.join("\n")}`; |
| 43 | + |
| 44 | + const editor = await this.ide.openUntitledTextDocument({ |
| 45 | + language: "markdown", |
| 46 | + }); |
| 47 | + |
| 48 | + const editableEditor = this.ide.getEditableTextEditor(editor); |
| 49 | + await editableEditor.insertSnippet(snippetText); |
| 50 | + } |
| 51 | + |
| 52 | + async saveActiveDocument() { |
| 53 | + const text = this.ide.activeTextEditor?.document.getText() ?? ""; |
| 54 | + const matchLanguageId = text.match(/^\[\[(\w+)\]\]\n/); |
| 55 | + |
| 56 | + if (matchLanguageId == null) { |
| 57 | + throw Error(`Can't match language id`); |
| 58 | + } |
| 59 | + |
| 60 | + const languageId = matchLanguageId[1]; |
| 61 | + const restText = text.slice(matchLanguageId[0].length); |
| 62 | + |
| 63 | + const parts = restText |
| 64 | + .split(/^---$/gm) |
| 65 | + .map((p) => p.trimStart()) |
| 66 | + .filter(Boolean); |
| 67 | + |
| 68 | + const facetsToAdd: { facet: string; content: string }[] = []; |
| 69 | + |
| 70 | + for (const part of parts) { |
| 71 | + const match = part.match(/^\[([\w.]+)\].*\n([\s\S]*)$/); |
| 72 | + const facet = match?.[1]; |
| 73 | + const content = match?.[2] ?? ""; |
| 74 | + |
| 75 | + if (facet == null) { |
| 76 | + throw Error(`Invalid pattern '${part}'`); |
| 77 | + } |
| 78 | + |
| 79 | + if (!content.trim()) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + facetsToAdd.push({ facet, content }); |
| 84 | + } |
| 85 | + |
| 86 | + const langDirectory = path.join(getScopeTestsDirPath(), languageId); |
| 87 | + |
| 88 | + await fsPromises.mkdir(langDirectory, { recursive: true }); |
| 89 | + |
| 90 | + for (const { facet, content } of facetsToAdd) { |
| 91 | + const fullContent = `${content}---\n`; |
| 92 | + let filePath = path.join(langDirectory, `${facet}.scope`); |
| 93 | + let i = 2; |
| 94 | + |
| 95 | + while (fs.existsSync(filePath)) { |
| 96 | + filePath = path.join(langDirectory, `${facet}${i++}.scope`); |
| 97 | + } |
| 98 | + |
| 99 | + await fsPromises.writeFile(filePath, fullContent, "utf-8"); |
| 100 | + } |
| 101 | + |
| 102 | + await showInfo( |
| 103 | + this.ide.messages, |
| 104 | + "scopeTestsSaved", |
| 105 | + `${facetsToAdd.length} scope tests saved for language '${languageId}`, |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + private languageSelection() { |
| 110 | + const languageIds = Object.keys(languageScopeSupport); |
| 111 | + languageIds.sort(); |
| 112 | + return this.ide.showQuickPick(languageIds, { |
| 113 | + title: "Select language to record scope tests for", |
| 114 | + }); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +function getSupportedScopeFacets(languageId: string): ScopeSupportFacet[] { |
| 119 | + const scopeSupport = languageScopeSupport[languageId]; |
| 120 | + |
| 121 | + if (scopeSupport == null) { |
| 122 | + throw Error(`Missing scope support for language '${languageId}'`); |
| 123 | + } |
| 124 | + |
| 125 | + const scopeFacets = Object.keys(scopeSupport) as ScopeSupportFacet[]; |
| 126 | + |
| 127 | + return scopeFacets.filter( |
| 128 | + (facet) => scopeSupport[facet] === ScopeSupportFacetLevel.supported, |
| 129 | + ); |
| 130 | +} |
| 131 | + |
| 132 | +function getExistingScopeFacetTest(languageId: string): Set<string> { |
| 133 | + const testPaths = getScopeTestPathsRecursively(); |
| 134 | + const languages = groupBy(testPaths, (test) => test.languageId); |
| 135 | + const testPathsForLanguage = languages.get(languageId) ?? []; |
| 136 | + const facets = testPathsForLanguage.map((test) => test.facet); |
| 137 | + return new Set(facets); |
| 138 | +} |
0 commit comments