Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
fix tree-sitter issue
Browse files Browse the repository at this point in the history
  • Loading branch information
znck committed Nov 2, 2023
1 parent cf8dcdc commit a1a115c
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 32 deletions.
1 change: 1 addition & 0 deletions extension/src/StatusBarController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class StatusBarController {
commands.registerCommand('grammarly.pauseCheck', async (uri?: Uri) => {
const id = uri ?? window.activeTextEditor?.document.uri
if (id == null) return

await this.grammarly.client.protocol.pause(id.toString())
await this.update()
}),
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"private": true,
"engines": {
"node": "^16.20.0",
"pnpm": "^8.1.1"
"node": "^18.18.2",
"pnpm": "^8.8.0"
},
"scripts": {
"build": "rollup -c && node scripts/build-web-extension.mjs",
Expand Down Expand Up @@ -32,8 +32,8 @@
"rollup": "^2.71.1",
"rollup-plugin-copy": "^3.4.0",
"semver": "^7.3.7",
"tree-sitter-cli": "^0.20.6",
"tree-sitter-html": "^0.19.0",
"tree-sitter-cli": "^0.20.8",
"tree-sitter-html": "^0.20.0",
"tree-sitter-markdown": "^0.7.1",
"tslib": "^2.4.0",
"typescript": "^4.6.4",
Expand Down
1 change: 1 addition & 0 deletions packages/grammarly-languageclient/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function createProtocol(client: BaseLanguageClient): Protocol {
} else {
return async (...args: unknown[]): Promise<unknown> => {
try {
await client.onReady()
const result = await client.sendRequest(`$/${property}`, args)
return result
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion packages/grammarly-languageserver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"reflect-metadata": "^0.1.13",
"vscode-languageserver": "^7.0.0",
"vscode-languageserver-textdocument": "^1.0.4",
"web-tree-sitter": "0.20.5"
"web-tree-sitter": "^0.20.8"
},
"devDependencies": {
"domhandler": "^5.0.3"
Expand Down
2 changes: 1 addition & 1 deletion packages/grammarly-richtext-encoder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"dist"
],
"dependencies": {
"web-tree-sitter": "0.20.5"
"web-tree-sitter": "^0.20.8"
},
"devDependencies": {
"@grammarly/sdk": "^2.3.17",
Expand Down
40 changes: 31 additions & 9 deletions packages/grammarly-richtext-encoder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,36 @@ export async function createParser(language: string): Promise<Parser> {
return await parser

async function createParserInner() {
await Parser.init()

const parser = new Parser()
parser.setLanguage(await Parser.Language.load(getLanguageFile()))
parsers.set(language, parser)
parsersPending.delete(language)
if (isNodeJS()) {
const fetch = globalThis.fetch
try {
// @ts-ignore
globalThis.fetch = null
await Parser.init()
} catch (e) {
console.log('Error in TreeSitter parser:', e)
throw e
} finally {
globalThis.fetch = fetch
}
} else {
await Parser.init()
}

return parser
try {
const parser = new Parser()
parser.setLanguage(await Parser.Language.load(getLanguageFile()))
parsers.set(language, parser)
parsersPending.delete(language)
return parser
} catch (e) {
console.log(`Error in TreeSitter ${language} parser:`, e)
throw e
}
}

function getLanguageFile(): string | Uint8Array {
// @ts-ignore - Ignore if process does not exist.
if (typeof process !== 'undefined' && process.versions?.node != null) {
if (isNodeJS()) {
// @ts-ignore
if (process.env.NODE_ENV === 'test') {
// @ts-ignore
Expand All @@ -42,4 +59,9 @@ export async function createParser(language: string): Promise<Parser> {
}
}

function isNodeJS(): boolean {
// @ts-ignore - Ignore if process does not exist.
return typeof process !== 'undefined' && process.versions?.node != null
}

export const transformers = { html, markdown } as const
File renamed without changes.
File renamed without changes.
38 changes: 23 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function wasm(file) {
return copy({
targets: [
{
src: Path.resolve(__dirname, 'tree-sitter/tree-sitter-{html,markdown}.wasm'),
src: Path.resolve(__dirname, 'parsers/tree-sitter-{html,markdown}.wasm'),
dest: Path.dirname(file),
},
],
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-wasm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ for (const language in packageNameByLanguage) {
execSync(`$(pnpm bin)/tree-sitter build-wasm ${resolve(rootDir, `node_modules/${packageName}`)}`, {
stdio: 'inherit',
})
execSync(`mv ${packageName}.wasm tree-sitter/tree-sitter-${language}.wasm`, { stdio: 'inherit' })
execSync(`mv ${packageName}.wasm parsers/tree-sitter-${language}.wasm`, { stdio: 'inherit' })
}

0 comments on commit a1a115c

Please sign in to comment.