Skip to content

Tweaked tree-sitter query classification #7431

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
18 changes: 10 additions & 8 deletions lib/linguist/heuristics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -737,21 +737,23 @@ disambiguations:
pattern: '^#+\s+(NAME|SYNOPSIS|DESCRIPTION)'
- extensions: ['.scm']
rules:
- language: Scheme
pattern:
- '(?:''[\(\*#]|\w->\w|\.\.\.[\s\)]|\([+\-:<>\/=~\)]|~>|[#`]\(|#:\w)'
- '^\s*\((?:define\*?|import|library|lambda)'
negative_pattern:
- '\(#[\w-]+[!\?]'
- '[\)\]"_]\s*(?:[\*\+\?]|@\w)'
- language: Tree-sitter Query
pattern:
- '\(#[\w-]+[!\?]'
- '[\)\]"_]\s*(?:[\*\+\?]|@\w)'
- '(?:[\)\]]\s*[\*\+\?](?:\s|$))'
- '(?:^\s*\w+:\s*[\(\[\"])'
- '\(#(?:set!|(?:not-)?(?:any-of|match)\?)'
- '@[\w.-]+(?:\s|$)'
negative_pattern:
- '\([+\-:<>\/=~\)]'
- language: Scheme
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving TSQ above Scheme somehow increased classification hit-rate 🤷

pattern:
- '(?:''[\(\*#]|\w->\w|\.\.\.[\s\)]|\([+\-:<>\/=~\)]|~>|[#`]\(|#:\w)'
- '^\s*\((?:define\*?|import|library|lambda)'
negative_pattern:
- '\(#[\w-]+[!\?]'
- '(?:[\)\]]\s*[\*\+\?](?:\s|$))'
- '@[\w.-]+(?:\s|$)'
- extensions: ['.sol']
rules:
- language: Solidity
Expand Down
6 changes: 6 additions & 0 deletions lib/linguist/languages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7704,6 +7704,12 @@ Tree-sitter Query:
- tsq
extensions:
- ".scm"
filenames:
- folds.scm
- highlights.scm
- indents.scm
- injections.scm
- locals.scm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need these as they all share the .scm extension which should be sufficient.

Suggested change
filenames:
- folds.scm
- highlights.scm
- indents.scm
- injections.scm
- locals.scm
filenames:
- folds.scm
- highlights.scm
- indents.scm
- injections.scm
- locals.scm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it out, it seems those lines change classification, before deletion for nvim-tresitter:

$ ./bin/github-linguist $doc/nvim-treesitter | rg '(Tree-sitter|Scheme)'
69.59%  1128396    Tree-sitter Query

After deleting those lines:

$ ./bin/github-linguist $doc/nvim-treesitter | rg '(Tree-sitter|Scheme)'
69.52%  1127317    Tree-sitter Query
0.07%   1079       Scheme

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would then indicate the heuristics are missing a scenario specifically in your repo or the samples are not sufficiently teaching the classifier. Keep in mind we’re aiming for good enough, not perfection as every repo is different. It’s also why we have overrides.

The use of specific files is for cases where a specific filename is always used by that language, and generally doesn’t have an extension or has a generic or very common extension associated with another language. Using a filename will mean every instance of that filename will be classified as that language, even if the file is legitimately written in another language. So if a Scala developer creates a file called locals.scm, they’ll be forced to use an override because you decided all files with this name should be Tree-sitter query.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation, the changed turned out to be a rather small tweak.

tm_scope: source.scm
ace_mode: text
language_id: 436081647
Expand Down
66 changes: 66 additions & 0 deletions samples/Tree-sitter Query/asm_highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
; General
(label
[
(ident)
(word)
] @label)

(reg) @variable.builtin

(meta
kind: (_) @function.builtin)

(instruction
kind: (_) @function.builtin)

(const
name: (word) @constant)

; Comments
[
(line_comment)
(block_comment)
] @comment @spell

; Literals
(int) @number

(float) @number.float

(string) @string

; Keywords
[
"byte"
"word"
"dword"
"qword"
"ptr"
"rel"
"label"
"const"
] @keyword

; Operators & Punctuation
[
"+"
"-"
"*"
"/"
"%"
"|"
"^"
"&"
] @operator

[
"("
")"
"["
"]"
] @punctuation.bracket

[
","
":"
] @punctuation.delimiter
5 changes: 5 additions & 0 deletions samples/Tree-sitter Query/asm_injections.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
([
(line_comment)
(block_comment)
] @injection.content
(#set! injection.language "comment"))
167 changes: 167 additions & 0 deletions samples/Tree-sitter Query/func_highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
; Include
"#include" @keyword.import

(include_path) @string

; Preproc
"#pragma" @keyword.directive

(pragma_directive
[
"version"
"not-version"
"test-version-set"
] @keyword.directive)

; Keywords
[
"asm"
"impure"
"inline"
"inline_ref"
"method_id"
"type"
] @keyword

"return" @keyword.return

; Conditionals
[
"if"
"ifnot"
"else"
"elseif"
"elseifnot"
"until"
] @keyword.conditional

; Exceptions
[
"try"
"catch"
] @keyword.exception

; Repeats
[
"do"
"forall"
"repeat"
"while"
] @keyword.repeat

; Qualifiers
[
"const"
"global"
(var)
] @keyword.modifier

; Variables
(identifier) @variable

; Constants
(const_var_declarations
name: (identifier) @constant)

; Functions/Methods
(function_definition
name: (function_name) @function)

(function_application
function: (identifier) @function)

(method_call
method_name: (identifier) @function.method.call)

; Parameters
(parameter) @variable.parameter

; Types
(type_identifier) @type

(primitive_type) @type.builtin

; Operators
[
"="
"+="
"-="
"*="
"/="
"~/="
"^/="
"%="
"~%="
"^%="
"<<="
">>="
"~>>="
"^>>="
"&="
"|="
"^="
"=="
"<"
">"
"<="
">="
"!="
"<=>"
"<<"
">>"
"~>>"
"^>>"
"-"
"+"
"|"
"^"
"*"
"/"
"%"
"~/"
"^/"
"~%"
"^%"
"/%"
"&"
"~"
] @operator

; Literals
[
(string)
(asm_instruction)
] @string

[
(string_type)
(underscore)
] @character.special

(number) @number

; Punctuation
[
"{"
"}"
] @punctuation.bracket

[
"("
")"
"()"
] @punctuation.bracket

[
"["
"]"
] @punctuation.bracket

[
";"
","
"->"
] @punctuation.delimiter

; Comments
(comment) @comment @spell
Loading