Skip to content

Commit dd2f65b

Browse files
feat: add registerDynamicLanguage to napi
1 parent 3a7ce28 commit dd2f65b

File tree

5 files changed

+50
-4
lines changed

5 files changed

+50
-4
lines changed

crates/napi/__test__/custom.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import test from 'ava'
2+
3+
import {
4+
registerDynamicLanguage,
5+
parse,
6+
} from '../index'
7+
8+
const { platform, arch } = process
9+
10+
const isAppleSilicon = platform === 'darwin' && arch === 'arm64'
11+
if (isAppleSilicon) {
12+
registerDynamicLanguage({
13+
myjson: {
14+
libraryPath: "../../benches/fixtures/json-mac.so",
15+
languageSymbol: "tree_sitter_json",
16+
extensions: ["myjson"],
17+
}
18+
})
19+
}
20+
21+
test('test load custom lang', t => {
22+
if (!isAppleSilicon) {
23+
t.pass('This test is not available on this platform')
24+
return
25+
}
26+
// @ts-expect-error TODO: change type
27+
const sg = parse('myjson', '{"test": 123}')
28+
const root = sg.root()
29+
const node = root.find("123")!
30+
t.truthy(node)
31+
t.is(node.kind(), 'number')
32+
const no = root.find("456")
33+
t.falsy(no)
34+
})

crates/napi/__test__/index.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,4 @@ test('find node by range', t => {
434434
})
435435
const byKind = sg.root().find(js.kind('template_string'))
436436
t.is(node!.id(), byKind!.id())
437-
})
437+
})

crates/napi/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ export {
1717
} from './types/api'
1818
// deprecated
1919
export * from './types/deprecated'
20+
21+
// TODO
22+
export declare function registerDynamicLanguage(langs: any): void

crates/napi/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ if (!nativeBinding) {
310310
throw new Error(`Failed to load native binding`)
311311
}
312312

313-
const { parseFiles, Lang, SgNode, SgRoot, parse, parseAsync, kind, pattern, findInFiles, html, js, jsx, ts, tsx, css } = nativeBinding
313+
const { parseFiles, Lang, SgNode, SgRoot, parse, parseAsync, kind, pattern, findInFiles, registerDynamicLanguage, html, js, jsx, ts, tsx, css } = nativeBinding
314314

315315
module.exports.parseFiles = parseFiles
316316
module.exports.Lang = Lang
@@ -321,6 +321,7 @@ module.exports.parseAsync = parseAsync
321321
module.exports.kind = kind
322322
module.exports.pattern = pattern
323323
module.exports.findInFiles = findInFiles
324+
module.exports.registerDynamicLanguage = registerDynamicLanguage
324325
module.exports.html = html
325326
module.exports.js = js
326327
module.exports.jsx = jsx

crates/napi/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ mod find_files;
55
mod napi_lang;
66
mod sg_node;
77

8-
use ast_grep_core::language::Language;
9-
use ast_grep_core::AstGrep;
8+
use ast_grep_core::{AstGrep, Language};
109
use ast_grep_language::SupportLang;
1110
use napi::bindgen_prelude::*;
1211
use napi_derive::napi;
12+
use napi_lang::register_dynamic_language as register_dynamic_language_impl;
1313

1414
use doc::{JsDoc, NapiConfig};
1515
use find_files::{find_in_files_impl, FindConfig, FindInFiles, ParseAsync};
@@ -119,3 +119,11 @@ pub fn find_in_files(
119119
let lang: NapiLang = lang.parse()?;
120120
find_in_files_impl(lang, config, callback)
121121
}
122+
123+
/// Register a dynamic language to ast-grep.
124+
/// `langs` is a Map of language name to its CustomLanguage registration.
125+
#[napi]
126+
pub fn register_dynamic_language(langs: serde_json::Value) -> Result<()> {
127+
let langs = serde_json::from_value(langs)?;
128+
register_dynamic_language_impl(langs)
129+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy