上一篇:使用Theia——創建插件
Theia——添加語言支持
Theia中TextMate的支持
使用TextMate語法可以為大部分源檔案提供精準的著色修飾,雖然這只是在語法級別上(沒有語言本身的深度決議),語意著色可以由語言服務器提供, TextMate語法主要有兩種格式:.plist和.tmLanguage.json,這兩種Theia都支持, 更多有關TextMate語法的內容可以查看這里, 注意:特定語言的語法應該包含在該語言的專用擴展包中,@theia/textmate-grammars中只注冊了當前沒有任何特定擴展包的語言,添加新語法
要提供一種新語法,通常的做法是在擴展包的根目錄下創建一個data目錄,在其中保存不同的語法,extension/ data/ grammars go here lib/ ... src/ ... package.json ...
然后,在package.json檔案中宣告以下屬性,這樣新提供的語法可以與源代碼和編譯的檔案一同發布,
"files": [ "data", "lib", "src" ],
在擴展包中,我們可以通過LanguageGrammarDefinitionContribution的contribution point來提供這一特性,
@injectable()export class YourContribution implements LanguageGrammarDefinitionContribution { readonly id = 'languageId'; readonly scopeName = 'source.yourLanguage'; registerTextmateLanguage(registry: TextmateRegisty) { registry.registerTextmateGrammarScope(this.scopeName, { async getGrammarDefinition() { return { format: 'json', content: require('../data/yourGrammar.tmLanguage.json'), } } }); registry.mapLanguageIdToTextmateGrammar(this.id, this.scopeName); }}
如果使用.plist語法,則不能使用require來直接獲取內容,因為Webpack將回傳從服務器獲取的檔案的名稱,這種情況下,可以使用下面的模式來獲取檔案的內容:
@injectable()export class YourContribution implements LanguageGrammarDefinitionContribution { readonly id = 'languageId'; readonly scopeName = 'source.yourLanguage'; registerTextmateLanguage(registry: TextmateRegisty) { registry.registerTextmateGrammarScope(this.scopeName, { async getGrammarDefinition() { const response = await fetch(require('../data/yourGrammar.plist')); return { format: 'plist', content: await response.text(), } } }); registry.mapLanguageIdToTextmateGrammar(this.id, this.scopeName); }}
原文地址:https://theia-ide.org/docs/textmate
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/6679.html
標籤:其他
上一篇:Theia架構
