aboutsummaryrefslogtreecommitdiff
path: root/data/plugins/language_python.lua
diff options
context:
space:
mode:
authorJefferson González <jgmdev@gmail.com>2025-06-24 21:13:02 -0400
committerGitHub <noreply@github.com>2025-06-24 21:13:02 -0400
commit6c8e8dcff2aec264aa002e48056c93a35016adb4 (patch)
tree1a93b594c015930648eabc7e6323d22d0762d1d0 /data/plugins/language_python.lua
parent4b6637d0be3171caadb0fd6977cecdaf01bf96af (diff)
downloadpragtical-6c8e8dcff2aec264aa002e48056c93a35016adb4.tar.gz
pragtical-6c8e8dcff2aec264aa002e48056c93a35016adb4.zip
Corrections to language_python (#278)
This is a continuation of #274 and fixes various of the reported issues on that PR without reverting to the older version of the language definition. Changes tested with a 40k LOC file and performance seems fine.
Diffstat (limited to 'data/plugins/language_python.lua')
-rw-r--r--data/plugins/language_python.lua170
1 files changed, 103 insertions, 67 deletions
diff --git a/data/plugins/language_python.lua b/data/plugins/language_python.lua
index 8d67f57e..0b0cb452 100644
--- a/data/plugins/language_python.lua
+++ b/data/plugins/language_python.lua
@@ -11,7 +11,6 @@ end
local python_symbols = {
-
["class"] = "keyword",
["finally"] = "keyword",
["is"] = "keyword",
@@ -52,98 +51,108 @@ local python_symbols = {
["None"] = "literal",
["True"] = "literal",
["False"] = "literal",
-
}
-
local python_fstring = {
-
patterns = {
- { pattern = "\\.", type = "string" },
+ { pattern = "\\.", type = "string" },
{ pattern = '[^"\\{}\']+', type = "string" }
-
},
symbols = {}
}
-
local python_patterns = {
+ { pattern = "#.*", type = "comment" },
- { pattern = '[uUrR]%f["]', type = "keyword" },
+ { pattern = '[uUrR]%f["]', type = "keyword" },
- { pattern = { '[ruU]?"""', '"""', '\\' }, type = "string" },
- { pattern = { "[ruU]?'''", "'''", '\\' }, type = "string" },
- { pattern = { '[ruU]?"', '"', '\\' }, type = "string" },
- { pattern = { "[ruU]?'", "'", '\\' }, type = "string" },
- { pattern = { 'f"', '"', "\\" }, type = "string", syntax = python_fstring },
- { pattern = { "f'", "'", "\\" }, type = "string", syntax = python_fstring },
+ { pattern = { '[ruU]?"""', '"""', '\\' }, type = "string" },
+ { pattern = { "[ruU]?'''", "'''", '\\' }, type = "string" },
+ { pattern = { '[ruU]?"', '"', '\\' }, type = "string" },
+ { pattern = { "[ruU]?'", "'", '\\' }, type = "string" },
- { pattern = "%d+[%d%.eE_]*", type = "number" },
- { pattern = "0[xboXBO][%da-fA-F_]+", type = "number" },
- { pattern = "%.?%d+", type = "number" },
- { pattern = "%f[-%w_]-%f[%d%.]", type = "number" },
+ { pattern = { 'f"', '"', "\\" },
+ type = "string", syntax = python_fstring
+ },
+ { pattern = { "f'", "'", "\\" },
+ type = "string",
+ syntax = python_fstring
+ },
- { pattern = "[%+%-=/%*%^%%<>!~|&]", type = "operator" },
- { pattern = "[%a_][%w_]*%f[(]", type = "function" },
+ { pattern = "%d+[%d%.eE_]*", type = "number" },
+ { pattern = "0[xboXBO][%da-fA-F_]+", type = "number" },
+ { pattern = "%.?%d+", type = "number" },
+ { pattern = "%f[-%w_]-%f[%d%.]", type = "number" },
- { pattern = "[%a_][%w_]+", type = "symbol" },
+ { pattern = "[%+%-=/%*%^%%<>!~|&:]", type = "operator" },
+ { pattern = "[%a_][%w_]*%f[(]", type = "function" },
+ { pattern = "[%a_][%w_]+", type = "symbol" },
}
-
local python_type = {
-
patterns = {
- { pattern = "|", type = "operator" },
- { pattern = "[%w_]+", type = "keyword2" },
- { pattern = "[%a_][%w_]+", type = "symbol" },
+ { pattern = "|", type = "operator" },
+ { pattern = "[%w_]+", type = "keyword2" },
+ { pattern = "[%a_][%w_]+", type = "symbol" },
},
symbols = {
["None"] = "literal"
}
}
+
-- Add this line after in order for the recursion to work.
-- Makes sure that the square brackets are well balanced when capturing the syntax
-- (in order to make something like this work: Tuple[Tuple[int, str], float])
-table.insert(python_type.patterns, 1, { pattern = { "%[", "%]" }, syntax = python_type })
-
+table.insert(
+ python_type.patterns, 1,
+ { pattern = { "%[", "%]" }, syntax = python_type }
+)
-- For things like this_list = other_list[a:b:c]
local not_python_type = {
-
patterns = python_patterns,
-
symbols = python_symbols
-
}
-table.insert(not_python_type.patterns, 1, { pattern = { "%[", "%]" }, syntax = not_python_type })
-table.insert(not_python_type.patterns, 1, { pattern = { "{", "}" }, syntax = not_python_type })
-
-table.insert(python_fstring.patterns, 1, { pattern = { "{", "}" }, syntax = not_python_type })
-
+table.insert(
+ not_python_type.patterns, 1,
+ { pattern = { "%[", "%]" }, syntax = not_python_type }
+)
+table.insert(
+ not_python_type.patterns, 1,
+ { pattern = { "{", "}" }, syntax = not_python_type }
+)
+table.insert(
+ python_fstring.patterns, 1,
+ { pattern = { "{", "}" }, syntax = not_python_type }
+)
local python_func = {
-
patterns = table_merge({
- { pattern = { "->", "%f[:]" }, type = "operator", syntax = python_type },
- { pattern = { ":%s*", "%f[^%[%]%w_]" }, syntax = python_type },
+ { pattern = { "->", "%f[:]" },
+ type = "operator",
+ syntax = python_type
+ },
+ { pattern = { ":%s*", "%f[^%[%]%w_]" }, syntax = python_type },
}, python_patterns),
symbols = python_symbols
}
-table.insert(python_func.patterns, 1, { pattern = { "%(", "%)" }, syntax = python_func })
-
+table.insert(
+ python_func.patterns, 1,
+ { pattern = { "%(", "%)" }, syntax = python_func }
+)
syntax.add {
@@ -155,34 +164,61 @@ syntax.add {
patterns = table_merge({
- { pattern = "#.*", type = "comment" },
- { pattern = { '^%s*"""', '"""' }, type = "comment" },
-
- { pattern = { "%[", "%]" }, syntax = not_python_type },
- { pattern = { "{", "}" }, syntax = not_python_type },
-
- { pattern = { "^%s*()def%f[%s]", ":" }, type = { "normal", "keyword" }, syntax = python_func }, -- this and the following prevent one-liner highlight bugs
-
- { pattern = { "^%s*()for%f[%s]", ":" }, type = { "normal", "keyword" }, syntax = not_python_type },
- { pattern = { "^%s*()if%f[%s]", ":" }, type = { "normal", "keyword" }, syntax = not_python_type },
- { pattern = { "^%s*()elif%f[%s]", ":" }, type = { "normal", "keyword" }, syntax = not_python_type },
- { pattern = { "^%s*()while%f[%s]", ":" }, type = { "normal", "keyword" }, syntax = not_python_type },
- { pattern = { "^%s*()match%f[%s]", ":" }, type = { "normal", "keyword" }, syntax = not_python_type },
- { pattern = { "^%s*()case%f[%s]", ":" }, type = { "normal", "keyword" }, syntax = not_python_type },
- { pattern = { "^%s*()except%f[%s]", ":" }, type = { "normal", "keyword" }, syntax = not_python_type },
-
- { pattern = "else():", type = { "keyword", "normal" } },
- { pattern = "try():", type = { "keyword", "normal" } },
-
- { pattern = "lambda()%s.+:", type = { "keyword", "normal" } },
- { pattern = "class%s+()[%a_][%w_]+().*:", type = { "keyword", "keyword2", "normal" } },
-
-
- { pattern = { ":%s*", "%f[^%[%]%w_]"}, syntax = python_type },
+ { pattern = "#.*", type = "comment" },
+ { pattern = { '^%s*"""', '"""' }, type = "comment" },
+
+ { pattern = { "%[", "%]" }, syntax = not_python_type },
+ { pattern = { "{", "}" }, syntax = not_python_type },
+
+ -- this and the following prevent one-liner highlight bugs
+ { pattern = { "^%s*()def%f[%s]", ":()%s*$" },
+ type = { "normal", "normal" },
+ syntax = python_func
+ },
+
+ { pattern = { "^%s*()for%f[%s]", ":()%s*$" },
+ type = { "normal", "normal" },
+ syntax = not_python_type
+ },
+ { pattern = { "^%s*()if%f[%s]", ":()%s*$" },
+ type = { "normal", "normal" },
+ syntax = not_python_type
+ },
+ { pattern = { "^%s*()elif%f[%s]", ":()%s*$" },
+ type = { "normal", "normal" },
+ syntax = not_python_type
+ },
+ { pattern = { "^%s*()while%f[%s]", ":()%s*$" },
+ type = { "normal", "normal" },
+ syntax = not_python_type
+ },
+ { pattern = { "^%s*()match%f[%s]", ":()%s*$" },
+ type = { "normal", "normal" },
+ syntax = not_python_type },
+ { pattern = { "^%s*()case%f[%s]", ":()%s*$" },
+ type = { "normal", "normal" },
+ syntax = not_python_type
+ },
+ { pattern = { "^%s*()except%f[%s]", ":()%s*$" },
+ type = { "normal", "normal" },
+ syntax = not_python_type
+ },
+
+ { pattern = "else():", type = { "keyword", "normal" } },
+ { pattern = "try():", type = { "keyword", "normal" } },
+
+ { pattern = "lambda()%s.+:", type = { "keyword", "normal" } },
+ { pattern = "class%s+()[%a_][%w_]+().*:",
+ type = { "keyword", "keyword2", "normal" }
+ },
+
+ { pattern = { ":()%s*'", "()'" },
+ type = { "normal", "string" },
+ syntax = python_type
+ },
+ { pattern = { ":%s*", "%f[^%[%]%w_]" }, syntax = python_type },
}, python_patterns),
symbols = python_symbols
-
}
-