aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Vigouroux <39092278+vigoux@users.noreply.github.com>2020-05-23 15:19:03 +0200
committerGitHub <noreply@github.com>2020-05-23 15:19:03 +0200
commit9afad9698ec395afb837908031ca03e3c6a7470b (patch)
treea27ba10c44ffcdf57de7e7b43104271687f2c49e
parentMerge pull request #61 from theHamsta/cleanup-postspace (diff)
parentIntroduce `@include` (diff)
downloadnvim-treesitter-9afad9698ec395afb837908031ca03e3c6a7470b.tar
nvim-treesitter-9afad9698ec395afb837908031ca03e3c6a7470b.tar.gz
nvim-treesitter-9afad9698ec395afb837908031ca03e3c6a7470b.tar.bz2
nvim-treesitter-9afad9698ec395afb837908031ca03e3c6a7470b.tar.lz
nvim-treesitter-9afad9698ec395afb837908031ca03e3c6a7470b.tar.xz
nvim-treesitter-9afad9698ec395afb837908031ca03e3c6a7470b.tar.zst
nvim-treesitter-9afad9698ec395afb837908031ca03e3c6a7470b.zip
Merge pull request #56 from theHamsta/python-locals
Add python locals.scm
-rw-r--r--CONTRIBUTING.md1
-rw-r--r--README.md2
-rw-r--r--lua/nvim-treesitter/highlight.lua1
-rw-r--r--queries/python/highlights.scm176
-rw-r--r--queries/python/locals.scm71
5 files changed, 250 insertions, 1 deletions
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 5335d04ea..ea31f573e 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -79,6 +79,7 @@ are optional and will not have any effect for now.
`@operator`
`@keyword`
`@exception`
+`@include` keywords for including modules (e.g. import/from in Python)
`@type`
`builtin`
diff --git a/README.md b/README.md
index d9d9b8f20..e87bc2adb 100644
--- a/README.md
+++ b/README.md
@@ -158,7 +158,7 @@ List of currently supported languages:
- [ ] go
- [ ] cpp
- [ ] rust
-- [ ] python
+- [x] python (maintained by @theHamsta)
- [ ] javascript
- [ ] typescript
- [ ] tsx
diff --git a/lua/nvim-treesitter/highlight.lua b/lua/nvim-treesitter/highlight.lua
index c1981aff8..ac46010bb 100644
--- a/lua/nvim-treesitter/highlight.lua
+++ b/lua/nvim-treesitter/highlight.lua
@@ -47,6 +47,7 @@ hlmap["exception"] = "Exception"
hlmap["type"] = "Type"
hlmap["type.builtin"] = "Type"
hlmap["structure"] = "Structure"
+hlmap["include"] = "Include"
function M.attach(bufnr, ft)
local buf = bufnr or api.nvim_get_current_buf()
diff --git a/queries/python/highlights.scm b/queries/python/highlights.scm
new file mode 100644
index 000000000..4cd8b8998
--- /dev/null
+++ b/queries/python/highlights.scm
@@ -0,0 +1,176 @@
+;; From tree-sitter-python licensed under MIT License
+; Copyright (c) 2016 Max Brunsfeld
+
+; Identifier naming conventions
+
+
+((import_from_statement
+ name: (dotted_name
+ (identifier)) @type)
+ (match? @type "^[A-Z]"))
+
+((identifier) @constant
+ (match? @constant "^[A-Z][A-Z_]*$"))
+
+; Function calls
+
+(decorator) @function
+
+(call
+ function: (attribute
+ attribute: (identifier) @method))
+
+(call
+ function: (identifier) @function)
+
+((call
+ (identifier) @constructor)
+ (match? @constructor "^[A-Z]"))
+
+;; Builtin functions
+
+((call
+ function: (identifier) @function.builtin)
+ (match?
+ @function.builtin
+ "^(abs|all|any|ascii|bin|bool|breakpoint|bytearray|bytes|callable|chr|classmethod|compile|complex|delattr|dict|dir|divmod|enumerate|eval|exec|filter|float|format|frozenset|getattr|globals|hasattr|hash|help|hex|id|input|int|isinstance|issubclass|iter|len|list|locals|map|max|memoryview|min|next|object|oct|open|ord|pow|print|property|range|repr|reversed|round|set|setattr|slice|sorted|staticmethod|str|sum|super|tuple|type|vars|zip|__import__)$"))
+
+;; Function definitions
+
+(function_definition
+ name: (identifier) @function)
+
+(identifier) @variable
+(attribute attribute: (identifier) @property)
+(type (identifier) @type)
+(parameters
+ (identifier) @parameter)
+
+; Literals
+
+(none) @constant.builtin
+(true) @boolean
+(false) @boolean
+((identifier) @constant.builtin
+ (match? @constant.builtin "self"))
+
+(integer) @number
+(float) @float
+
+(comment) @comment
+(string) @string
+(escape_sequence) @escape
+
+(interpolation
+ "{" @punctuation.special
+ "}" @punctuation.special) @embedded
+
+; Tokens
+
+"-" @operator
+"-=" @operator
+"!=" @operator
+"*" @operator
+"**" @operator
+"**=" @operator
+"*=" @operator
+"/" @operator
+"//" @operator
+"//=" @operator
+"/=" @operator
+"&" @operator
+"%" @operator
+"%=" @operator
+"^" @operator
+"+" @operator
+"+=" @operator
+"<" @operator
+"<<" @operator
+"<=" @operator
+"<>" @operator
+"=" @operator
+"==" @operator
+">" @operator
+">=" @operator
+">>" @operator
+"|" @operator
+"~" @operator
+"and" @operator
+"in" @operator
+"is" @operator
+"not" @operator
+"or" @operator
+
+; Keywords
+
+"as" @include
+"assert" @keyword
+"async" @keyword
+"await" @keyword
+"break" @repeat
+"class" @keyword
+"continue" @repeat
+"def" @keyword
+"del" @keyword
+"elif" @conditional
+"else" @conditional
+"except" @keyword
+"exec" @keyword
+"finally" @keyword
+"for" @repeat
+"from" @include
+"global" @keyword
+"if" @conditional
+"import" @include
+"lambda" @keyword
+"nonlocal" @keyword
+"pass" @keyword
+"print" @keyword
+"raise" @keyword
+"return" @keyword
+"try" @keyword
+"while" @repeat
+"with" @keyword
+"yield" @keyword
+
+; Additions for nvim-treesitter
+"(" @punctuation.bracket
+")" @punctuation.bracket
+"[" @punctuation.bracket
+"]" @punctuation.bracket
+
+"," @punctuation.delimiter
+"." @punctuation.delimiter
+":" @punctuation.delimiter
+
+(class_definition
+ name: (identifier) @type)
+
+(attribute
+ attribute: (identifier) @field)
+
+((attribute
+ attribute: (identifier) @constant)
+ (match? @constant "^[A-Z][A-Z_]*$"))
+
+((attribute
+ attribute: (identifier) @type)
+ (match? @type "^[A-Z][a-z_]+"))
+
+(class_definition
+ body: (block
+ (expression_statement
+ (assignment
+ left: (expression_list
+ (identifier) @field)))))
+
+((class_definition
+ body: (block
+ (expression_statement
+ (assignment
+ left: (expression_list
+ (identifier) @constant)))))
+ (match? @constant "^[A-Z][A-Z_]*$"))
+
+;; Error
+(ERROR) @error
diff --git a/queries/python/locals.scm b/queries/python/locals.scm
new file mode 100644
index 000000000..bc33d306b
--- /dev/null
+++ b/queries/python/locals.scm
@@ -0,0 +1,71 @@
+;;; Programm structure
+(module) @scope
+
+(class_definition
+ body: (block
+ (expression_statement
+ (assignment
+ left: (expression_list
+ (identifier) @definition.associated))))) @scope
+
+; Function with parameters, defines parameters
+(function_definition
+ name: (identifier)
+ parameters: (parameters
+ (identifier) @definition.var))
+
+; Function defines function and scope
+(function_definition
+ name: (identifier) @definition.function) @scope
+
+;; Should be extended to when syntax supported
+;(function_definition
+ ;name: (identifier) @definition.function
+ ;body: (block (expression_statement (string) @definition.function.doc)?)) @scope
+
+
+(class_definition
+ name: (identifier) @definition.type) @scope
+
+(class_definition
+ body: (block
+ (function_definition
+ name: (identifier) @definition.method)))
+
+;;; Loops
+; not a scope!
+(for_statement
+ left: (variables
+ (identifier) @definition.var))
+
+; not a scope!
+;(while_statement) @scope
+
+; for in list comprehension
+(for_in_clause
+ left: (variables
+ (identifier) @definition.var))
+
+(dictionary_comprehension) @scope
+(list_comprehension) @scope
+(set_comprehension) @scope
+
+;;; Assignments
+
+(assignment
+ left: (expression_list
+ (identifier) @definition.var))
+
+(assignment
+ left: (expression_list
+ (attribute
+ (identifier)
+ (identifier) @definition.field)))
+
+; Walrus operator x := 1
+(named_expression
+ (identifier) @definition.var)
+
+
+;;; REFERENCES
+(identifier) @reference