Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: go field identifier #7

Merged
merged 11 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions crates/core/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12560,3 +12560,26 @@ fn go_import_metavariable() {
})
.unwrap();
}

#[test]
fn go_field_identifier() {
run_test_match(TestArg {
pattern: r#"
|language go
|
|`ReadFile`
|"#
.trim_margin()
.unwrap(),
source: r#"
|import (
| "io/ioutil"
|)
|
|file := ioutil.ReadFile("file.txt")
|"#
.trim_margin()
.unwrap(),
})
.unwrap();
}
2 changes: 1 addition & 1 deletion crates/language/src/go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Language for Go {
"Go"
}
fn snippet_context_strings(&self) -> &[(&'static str, &'static str)] {
&[("", ""), ("", ";")]
&[("", ""), ("", ";"), ("GO_OBJECT.", ";")]
}

fn node_types(&self) -> &[Vec<Field>] {
Expand Down
Binary file modified crates/wasm-bindings/wasm_parsers/tree-sitter-go.wasm
Binary file not shown.
22 changes: 15 additions & 7 deletions resources/edit_grammars.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,16 @@ const copyNodeTypes = async (lang, dest) =>
const copyWasmParser = async (lang, prefix) =>
fs.rename(
`${prefix ?? 'tree-sitter-'}${lang}/tree-sitter-${lang}.wasm`,
`../../wasm-bindings/wasm_parsers/tree-sitter-${lang}.wasm`,
`../../crates/wasm-bindings/wasm_parsers/tree-sitter-${lang}.wasm`,
);

async function rsyncGrammars(language) {
//If a language is given, only sync that language
//Otherwise, rm -rf the entire language-metavariables dir and sync it from scratch
const treeSitterLang = language ? `tree-sitter-${language}` : '.';
const mvDir = language ? `${LANGUAGE_METAVARIABLES}/${treeSitterLang}` : LANGUAGE_METAVARIABLES;
const mvDir = language
? `${LANGUAGE_METAVARIABLES}/${treeSitterLang}`
: LANGUAGE_METAVARIABLES;

if (languagesWithoutMetaVariables.includes(language)) {
return;
Expand All @@ -134,7 +136,9 @@ async function rsyncGrammars(language) {
'tree-sitter-*/corpus',
];
await execPromise(
`rsync -r -l ${submodulesDir} language-metavariables --exclude={${blobsToExclude.join(',')}}`,
`rsync -r -l ${submodulesDir} language-metavariables --exclude={${blobsToExclude.join(
','
)}}`
);
}

Expand All @@ -158,7 +162,8 @@ async function buildLanguage(language) {
throw `Unsupported language ${language}`;
}

const log = (message, ...args) => console.log(`[${language}] ` + message, ...args);
const log = (message, ...args) =>
console.log(`[${language}] ` + message, ...args);
log(`Starting`);
const tsLangDir = `tree-sitter-${language}`;
//Force cargo.toml to use the correct version of tree-sitter
Expand Down Expand Up @@ -189,12 +194,12 @@ async function buildLanguage(language) {

await fs.rename(
`tree-sitter-markdown/tree-sitter-markdown/tree-sitter-markdown.wasm`,
`../../wasm-bindings/wasm_parsers/tree-sitter-markdown-block.wasm`,
`../../crates/wasm-bindings/wasm_parsers/tree-sitter-markdown-block.wasm`,
);

await fs.rename(
`tree-sitter-markdown/tree-sitter-markdown-inline/tree-sitter-markdown_inline.wasm`,
`../../wasm-bindings/wasm_parsers/tree-sitter-markdown_inline.wasm`,
`../../crates/wasm-bindings/wasm_parsers/tree-sitter-markdown_inline.wasm`,
);
} else if (language === 'typescript') {
// typescript is special
Expand Down Expand Up @@ -243,7 +248,10 @@ async function buildLanguage(language) {
//SQL's wasm build hangs so we skip it
await treeSitterGenerate(language, false);
await copyNodeTypes(language);
await fs.copyFile(`${METAVARIABLE_GRAMMARS}/c_build.rs`, `${tsLangDir}/bindings/rust/build.rs`);
await fs.copyFile(
`${METAVARIABLE_GRAMMARS}/c_build.rs`,
`${tsLangDir}/bindings/rust/build.rs`,
);
} else {
await buildSimpleLanguage(log, language);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fn main() {
let mut c_config = cc::Build::new();
c_config.include(src_dir);
c_config
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-w")
.flag_if_supported("-Wno-unused-but-set-variable")
.flag_if_supported("-Wno-trigraphs");
let parser_path = src_dir.join("parser.c");
Expand Down
14 changes: 7 additions & 7 deletions resources/language-metavariables/tree-sitter-go/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module.exports = grammar({
inline: $ => [
$._type,
$._type_identifier,
$._field_identifier,
$.field_identifier,
$._package_identifier,
$._top_level_declaration,
$._string_literal,
Expand Down Expand Up @@ -211,7 +211,7 @@ module.exports = grammar({
method_declaration: $ => prec.right(1, seq(
'func',
field('receiver', $.parameter_list),
field('name', $._field_identifier),
field('name', $.field_identifier),
field('parameters', $.parameter_list),
field('result', optional(choice($.parameter_list, $._simple_type))),
field('body', optional($.block))
Expand Down Expand Up @@ -269,7 +269,7 @@ module.exports = grammar({
field('type', $._type)
),

field_name_list: $ => commaSep1(field('names', $._field_identifier)),
field_name_list: $ => commaSep1(field('names', $.field_identifier)),

expression_list: $ => commaSep1(field('expressions', $._expression)),

Expand Down Expand Up @@ -346,7 +346,7 @@ module.exports = grammar({
field_declaration: $ => seq(
choice(
seq(
commaSep1(field('name', $._field_identifier)),
commaSep1(field('name', $.field_identifier)),
field('type', $._type)
),
seq(
Expand Down Expand Up @@ -398,7 +398,7 @@ module.exports = grammar({
)),

method_spec: $ => seq(
field('name', $._field_identifier),
field('name', $.field_identifier),
field('parameters', $.parameter_list),
field('result', optional(choice($.parameter_list, $._simple_type)))
),
Expand Down Expand Up @@ -765,7 +765,7 @@ module.exports = grammar({
selector_expression: $ => prec(PREC.primary, seq(
field('operand', $._expression),
'.',
field('field', $._field_identifier)
field('field', $.field_identifier)
)),

index_expression: $ => prec(PREC.primary, seq(
Expand Down Expand Up @@ -904,7 +904,7 @@ module.exports = grammar({
identifier: $ => choice($._identifier, $.grit_metavariable),

_type_identifier: $ => alias($.identifier, $.type_identifier),
_field_identifier: $ => alias($.identifier, $.field_identifier),
field_identifier: $ => alias($.identifier, $.field_identifier),
_package_identifier: $ => alias($.identifier, $.package_identifier),

_string_literal: $ => choice(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@
"name": "name",
"content": {
"type": "SYMBOL",
"name": "_field_identifier"
"name": "field_identifier"
}
},
{
Expand Down Expand Up @@ -1118,7 +1118,7 @@
"name": "names",
"content": {
"type": "SYMBOL",
"name": "_field_identifier"
"name": "field_identifier"
}
},
{
Expand All @@ -1135,7 +1135,7 @@
"name": "names",
"content": {
"type": "SYMBOL",
"name": "_field_identifier"
"name": "field_identifier"
}
}
]
Expand Down Expand Up @@ -1565,7 +1565,7 @@
"name": "name",
"content": {
"type": "SYMBOL",
"name": "_field_identifier"
"name": "field_identifier"
}
},
{
Expand All @@ -1582,7 +1582,7 @@
"name": "name",
"content": {
"type": "SYMBOL",
"name": "_field_identifier"
"name": "field_identifier"
}
}
]
Expand Down Expand Up @@ -1917,7 +1917,7 @@
"name": "name",
"content": {
"type": "SYMBOL",
"name": "_field_identifier"
"name": "field_identifier"
}
},
{
Expand Down Expand Up @@ -3912,7 +3912,7 @@
"name": "field",
"content": {
"type": "SYMBOL",
"name": "_field_identifier"
"name": "field_identifier"
}
}
]
Expand Down Expand Up @@ -4788,7 +4788,7 @@
"named": true,
"value": "type_identifier"
},
"_field_identifier": {
"field_identifier": {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
Expand Down Expand Up @@ -7295,7 +7295,7 @@
"inline": [
"_type",
"_type_identifier",
"_field_identifier",
"field_identifier",
"_package_identifier",
"_top_level_declaration",
"_string_literal"
Expand Down
Loading
Loading