Open JaKXz opened 1 year ago
I'd also be interested in either passing a custom grammar or some guidance for how to go about adding languages "officially". I'd love to be able to use Code Hike for PromQL educational stuff and there's an old PromQL textmate grammar at https://github.com/prometheus-community/vscode-promql/blob/master/syntaxes/promql.tmlanguage.yml that I could revive. Is Shiki the right place to contribute, or would https://github.com/code-hike/lighter be (if there is interest in adding new langauges at all)?
Any thoughts on implementing this? I'm happy to assist, but didn't want to work my way toward a solution if maintainers have a specific implementation in mind.
This is something I want to add. I'm working on v1.0, so it will have to wait until I release that, which may take some time.
As in, work shouldn't start until v1 is released because of churn? Or you'd like to consider this for v1?
As in, work shouldn't start until v1 is released because of churn? Or you'd like to consider this for v1?
As in: I want to include it in v1.0
@lachieh @juliusv can you share your custom grammars and sample code so I can test this with real world scenarios?
Sure thing! Here's a grammar for the WIT
WebAssembly Interface Types language, and a code sample to match:
The sample is my own but the grammar comes from the vscode-wit
extension here:
https://github.com/bytecodealliance/vscode-wit/blob/main/syntaxes/wit.tmLanguage.json
They also have a boatload of test wit files in their repo if you need more: https://github.com/bytecodealliance/vscode-wit/tree/main/tests/grammar/integration
@pomber I am also very much interested in custom Grammer. Currently the r
Grammer is rather incomplete. I have also played around with the theme editor, which is very helpful, but before I continue with a custom theme I would first like to work on the custom Grammer. Generally speaking is there any kind of tool or process you know of that can be used to create custom a Grammer? It seems a rather manual process by trial and error.
@FlippieCoetser the grammar for R comes from the shiki source which is a copy from the R VSCode extension source. I'd suggest making an issue and/or contributing a PR to the REditorSupport/vscode-R repo if you want to make improvements to the R grammar.
As for writing your own custom grammars, I have found the VSCode Syntax Highlight Guide to have a lot of helpful resources. Notably, the scope inspector tool is great for debugging.
@lachieh thanks for the help and direction! I will do as suggested.
I managed to get this working using the following steps. I added all these to my postinstall
script in package.json
node_modules/@code-hike/lighter/grammars
LANG_NAMES
using the following: require("@code-hike/lighter").LANG_NAMES.push("polar");
in my next.config.js
@code-hike/lighter
's source code to reference the new grammar, there are a couple of internal objects that list all supported languages that need to be modified:const fs = require("fs");
const sourceFile = "./node_modules/@code-hike/lighter/dist/index.cjs.js";
const lighterCode = fs.readFileSync(sourceFile, "utf8");
const lines = lighterCode.split("\n");
// Add polar to `aliasOrIdToScope` entry, which isn't exported
const aliasOrIdToScopeIndex = lines.findIndex(line => line.trim() === 'const aliasOrIdToScope = {');
if (aliasOrIdToScopeIndex === -1) {
throw new Error("Target line not found");
}
lines.splice(aliasOrIdToScopeIndex + 1, 0, "'polar': 'source.polar',");
// Add polar to `scopeToLanguageData`, which isn't exported
const scopeToLanguageDataIndex = lines.findIndex(line => line.trim() === 'const scopeToLanguageData = {');
if (scopeToLanguageDataIndex === -1) {
throw new Error("Target line not found");
}
lines.splice(scopeToLanguageDataIndex + 1, 0, "'source.polar':{id:'polar',path:'polar.tmLanguage.json',embeddedScopes: []},");
fs.writeFileSync(sourceFile, lines.join("\n"));
Reference: https://shiki.matsu.io/