-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
6dd6786
commit 1060e20
Showing
5 changed files
with
147 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { | ||
window, | ||
extensions | ||
} from 'vscode'; | ||
import { | ||
SnippetLanguage, | ||
SnippetFile, | ||
Snippet | ||
} from './snippets' | ||
import * as jsonc from 'jsonc-parser'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
export class SnippetLoader { | ||
|
||
private _snippets: {[language: string]: Snippet[]} = {}; | ||
private _snippetFiles: SnippetFile[] = new Array<SnippetFile>(); | ||
private _snippetExtensions: {[extensionId: string]: Snippet[]} = {}; | ||
|
||
constructor() { | ||
} | ||
|
||
refresh(clearSnippets: boolean): void { | ||
if (clearSnippets) { | ||
this._snippets = {}; | ||
this._snippetExtensions = {}; | ||
this._snippetFiles = []; | ||
} | ||
} | ||
|
||
async getSnippetLanguages(): Promise<SnippetLanguage[]> { | ||
// get snippet languages from extension snippet files | ||
const snippetLanguages: SnippetLanguage[] = []; | ||
const snippetLanguageMap: Map<string, SnippetLanguage> = new Map<string, SnippetLanguage>(); | ||
extensions.all.forEach(extension => { | ||
if (!extension.packageJSON.isBuiltin && extension.packageJSON?.contributes?.snippets) { | ||
const extensionName = extension.packageJSON?.extensionName; | ||
const extensionLocation = extension.packageJSON?.extensionLocation; | ||
const snippetsConfig = extension.packageJSON?.contributes?.snippets; | ||
if (extensionLocation && Array.isArray(snippetsConfig)) { | ||
snippetsConfig.forEach(snippetConfig => { | ||
const language: string = snippetConfig.language; | ||
const snippetFile: SnippetFile = new SnippetFile(extensionName, | ||
path.join(extensionLocation.fsPath, snippetConfig.path), | ||
language | ||
); | ||
if (!snippetLanguageMap.has(language)) { | ||
const snippetLanguage: SnippetLanguage = new SnippetLanguage(language); | ||
snippetLanguages.push(snippetLanguage); | ||
snippetLanguageMap.set(language, snippetLanguage); | ||
} | ||
snippetLanguageMap.get(language)?.snippetFiles.push(snippetFile); | ||
}); | ||
} | ||
} | ||
}); | ||
return Promise.resolve(snippetLanguages); | ||
} | ||
|
||
async getSnippetFiles(extensionId: string): Promise<SnippetFile[]> { | ||
const extension = extensions.getExtension(extensionId); | ||
let snippetFiles: SnippetFile[] = []; | ||
if (!this._snippetExtensions[extensionId]) { | ||
this._snippetExtensions[extensionId] = []; | ||
} | ||
if (extension) { | ||
const extensionLocation = extension.packageJSON?.extensionLocation; | ||
const snippetsConfig = extension.packageJSON?.contributes?.snippets; | ||
if (extensionLocation && Array.isArray(snippetsConfig)) { | ||
snippetsConfig.forEach(snippetConfig => { | ||
const snippetFile: SnippetFile = new SnippetFile( | ||
snippetConfig.language, | ||
path.join(extensionLocation.fsPath, snippetConfig.path), | ||
snippetConfig.language | ||
); | ||
snippetFiles.push(snippetFile); | ||
this._snippetFiles.push(snippetFile); | ||
}); | ||
await Promise.all(snippetFiles.map((file: SnippetFile) => this.getSnippets(file, extensionId))); | ||
} | ||
} | ||
return Promise.resolve(snippetFiles); | ||
} | ||
|
||
async getSnippets(snippetFile: SnippetFile, extensionId?: string): Promise<Snippet[]> { | ||
if (!this._snippets[snippetFile.language]) { | ||
// create new language snippets array | ||
this._snippets[snippetFile.language] = []; | ||
} | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(snippetFile.filePath, 'utf8', (error, snippetsConfig) => { | ||
if (error) { | ||
window.showErrorMessage(`Error reading file ${snippetFile.filePath} \n ${error.message}`); | ||
return reject([]); | ||
} | ||
if (snippetsConfig === '') { | ||
return resolve([]); | ||
} | ||
|
||
let parsedSnippets: any; | ||
try { | ||
parsedSnippets = jsonc.parse(snippetsConfig); // tslint:disable-line | ||
} | ||
catch (err) { | ||
window.showErrorMessage(`JSON parsing of snippet file ${snippetFile.filePath} failed`); | ||
return reject([]); | ||
} | ||
|
||
// load parsed snippets | ||
const snippets: Snippet[] = []; | ||
for (const key in parsedSnippets) { | ||
const parsedSnippet = parsedSnippets[key]; | ||
const scope = [snippetFile.language]; | ||
const snippet: Snippet = | ||
new Snippet(key, parsedSnippet.prefix, scope, parsedSnippet.body, snippetFile); | ||
snippets.push(snippet); | ||
this._snippets[snippetFile.language].push(snippet); | ||
if (extensionId) { | ||
this._snippetExtensions[extensionId].push(snippet); | ||
} | ||
} | ||
return resolve(snippets); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters