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

Add .zprofile and .zshrc to language_sh and fix header pattern. #470

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Samueru-sama
Copy link
Contributor

Now .zshrc gets recognized and scripts that have flags in their shebang also work.

However my .zprofile still doesn't work 🤔

When completed this will fix all the issues at #469

@Samueru-sama
Copy link
Contributor Author

Alright I think this is ready now.

I'm not very sure how this fixed this issue I was having here, but it works lol

I've also tested it with other scripts to make sure this doesn't introduce a bug and haven't noticed anything odd.

@Samueru-sama Samueru-sama marked this pull request as ready for review July 29, 2024 19:40
@Guldoman
Copy link
Member

I'm not very sure how this fixed this issue I was having here, but it works lol

It's because of nested ${...}.
With .- it takes as few characters as possible, with .* it takes as many as possible.
So with .-, it stops at the first }, while with .* it stops at the last.
This is problematic, because it will now match things like ${wq${as}asd} asdasd ${saasasd} wrongly.

What do you think about something like:

index 83e98b5..8396464 100644
--- a/plugins/language_sh.lua
+++ b/plugins/language_sh.lua
@@ -1,6 +1,16 @@
 -- mod-version:3
 local syntax = require "core.syntax"
 
+local string_syntax = {
+  name = "Shell script string",
+  patterns = {
+    { pattern = {"%${", "}"}, type = "keyword2", syntax = ".sh" },
+    { pattern = "[^%$]",      type = "string" },
+    { pattern = "%$[^{]",     type = "string" },
+  },
+  symbols = {}
+}
+
 syntax.add {
   name = "Shell script",
   files = { "%.sh$", "%.bash$", "%.bashrc$", "%.bash_profile$", "%.profile$", "%.zprofile$", "%.zsh$", "%.zshrc$", "%.fish$" },
@@ -11,11 +21,11 @@ syntax.add {
     -- as a comment.
     { pattern = "$[%a_@*#][%w_]*",                type = "keyword2" },
     -- Comments
-    { pattern = "#.*\n",                          type = "comment"  },
+    { pattern = "#.*",                            type = "comment"  },
     -- Strings
-    { pattern = { '"', '"', '\\' },               type = "string"   },
-    { pattern = { "'", "'", '\\' },               type = "string"   },
-    { pattern = { '`', '`', '\\' },               type = "string"   },
+    { pattern = { '"', '"', '\\' },               type = "string", syntax = string_syntax },
+    { pattern = { "'", "'", '\\' },               type = "string"                         },
+    { pattern = { '`', '`', '\\' },               type = "string", syntax = ".sh"         },
     -- Ignore numbers that start with dots or slashes
     { pattern = "%f[%w_%.%/]%d[%d%.]*%f[^%w_%.]", type = "number"   },
     -- Operators
@@ -35,12 +45,14 @@ syntax.add {
     -- Match variable assignments
     { pattern = "[_%a][%w_]+%f[%+=]",              type = "keyword2" },
     -- Match variable expansions
-    { pattern = "${.*}",                           type = "keyword2" },
+    { pattern = { "${", "}", "\\" },               type = "keyword2", syntax = ".sh"      },
     { pattern = "$[%d$%a_@*][%w_]*",               type = "keyword2" },
     -- Functions
     { pattern = "[%a_%-][%w_%-]*[%s]*%f[(]",       type = "function" },
     -- Everything else
     { pattern = "[%a_][%w_]*",                     type = "symbol"   },
+    -- Catch escaped } - to avoid closing a ${...\}...} prematurely
+    { pattern = "\\}",                             type = "normal"   },
   },
   symbols = {
     ["case"]      = "keyword",

Also, would you mind bumping the plugin version in manifest.json?

@Samueru-sama
Copy link
Contributor Author

It's because of nested ${...}.
With .- it takes as few characters as possible, with .* it takes as many as possible.
So with .-, it stops at the first }, while with .* it stops at the last.
This is problematic, because it will now match things like ${wq${as}asd} asdasd ${saasasd} wrongly.

Hi, thanks for the explanation but I'm still not sure why it fixes the issue 😅

I know that .* will match more than .-, but if that is the case, why does .- cause all functions in this script to be matched?

I know the problem is line 5 in the script, something happens that makes it seem like the last } wasn't there I think.

If it was the other way around, that .* had the issue and .- fixes it it would make sense, but anyway I just tested the changes you gave (I applied them to the file of my fork just in case) and something broke.

This is how part of the bemoji script looks with the changes I did:

image

And this is how it looks with the proposed changes:

image

Also, would you mind bumping the plugin version in manifest.json?

Will do!

@Guldoman
Copy link
Member

Guldoman commented Jul 30, 2024

The issue is caused by # here https://github.com/marty-oehme/bemoji/blob/cc9f809446f75a67e2128749537c51dd9558eec8/bemoji#L81.

So I guess we can't just say that inside ${...} it nests the entire syntax. We'll have to make one on purpose for it.
Something like:

index 83e98b5..bbd93f1 100644
--- a/plugins/language_sh.lua
+++ b/plugins/language_sh.lua
@@ -1,6 +1,26 @@
 -- mod-version:3
 local syntax = require "core.syntax"
 
+local expansion_syntax = {
+  name = "Shell script parameter expansion",
+  symbols = {}
+}
+expansion_syntax.patterns = {
+    { pattern = { "%${", "}", "\\" }, type = "keyword2", syntax = expansion_syntax },
+    { pattern = "[^%$}]+",            type = "string" },
+    { pattern = "[%$}]",              type = "string" },
+}
+
+local string_syntax = {
+  name = "Shell script string",
+  patterns = {
+    { pattern = { "%${", "}", "\\" }, type = "keyword2", syntax = expansion_syntax },
+    { pattern = '[^%$"]+',            type = "string" },
+    { pattern = '[%$"]',              type = "string" },
+  },
+  symbols = {}
+}
+
 syntax.add {
   name = "Shell script",
   files = { "%.sh$", "%.bash$", "%.bashrc$", "%.bash_profile$", "%.profile$", "%.zprofile$", "%.zsh$", "%.zshrc$", "%.fish$" },
@@ -11,11 +31,11 @@ syntax.add {
     -- as a comment.
     { pattern = "$[%a_@*#][%w_]*",                type = "keyword2" },
     -- Comments
-    { pattern = "#.*\n",                          type = "comment"  },
+    { pattern = "#.*",                            type = "comment"  },
     -- Strings
-    { pattern = { '"', '"', '\\' },               type = "string"   },
-    { pattern = { "'", "'", '\\' },               type = "string"   },
-    { pattern = { '`', '`', '\\' },               type = "string"   },
+    { pattern = { '"', '"', '\\' },               type = "string", syntax = string_syntax },
+    { pattern = { "'", "'", '\\' },               type = "string"                         },
+    { pattern = { '`', '`', '\\' },               type = "string", syntax = ".sh"         },
     -- Ignore numbers that start with dots or slashes
     { pattern = "%f[%w_%.%/]%d[%d%.]*%f[^%w_%.]", type = "number"   },
     -- Operators
@@ -35,7 +55,7 @@ syntax.add {
     -- Match variable assignments
     { pattern = "[_%a][%w_]+%f[%+=]",              type = "keyword2" },
     -- Match variable expansions
-    { pattern = "${.*}",                           type = "keyword2" },
+    { pattern = { "${", "}", '\\' },               type = "keyword2", syntax = expansion_syntax },
     { pattern = "$[%d$%a_@*][%w_]*",               type = "keyword2" },
     -- Functions
     { pattern = "[%a_%-][%w_%-]*[%s]*%f[(]",       type = "function" },

In the future we might even improve the expansion syntax, to highlight inside it too.

@Samueru-sama
Copy link
Contributor Author

That one works fine, thanks! I will apply the patch.

In the future we might even improve the expansion syntax, to highlight inside it too.

something I would like to see is variables inside " " get highlighted a different color, right now if you have a path with a variable it all has the same color.

@Guldoman
Copy link
Member

something I would like to see is variables inside " " get highlighted a different color

Something like this maybe:

index 8f38ca8..a043eb2 100644
--- a/plugins/language_sh.lua
+++ b/plugins/language_sh.lua
@@ -7,16 +7,18 @@ local expansion_syntax = {
 }
 expansion_syntax.patterns = {
     { pattern = { "%${", "}", "\\" }, type = "keyword2", syntax = expansion_syntax },
-    { pattern = "[^%$}]+",            type = "string" },
-    { pattern = "[%$}]",              type = "string" },
+    { pattern = "$[%d$%a_@*][%w_]*",  type = "keyword2" },
+    { pattern = "[^%$}]+",            type = "string"   },
+    { pattern = "[%$}]",              type = "string"   },
 }
 
 local string_syntax = {
   name = "Shell script string",
   patterns = {
     { pattern = { "%${", "}", "\\" }, type = "keyword2", syntax = expansion_syntax },
-    { pattern = '[^%$"]+',            type = "string" },
-    { pattern = '[%$"]',              type = "string" },
+    { pattern = "$[%d$%a_@*][%w_]*",  type = "keyword2" },
+    { pattern = '[^%$"]+',            type = "string"   },
+    { pattern = '[%$"]',              type = "string"   },
   },
   symbols = {}
 }

We should probably also add something about $(...).

@Samueru-sama
Copy link
Contributor Author

Samueru-sama commented Jul 30, 2024

That works fine, but I had to change my color profile in lite-xl because the one I had been using doesn't differentiate the change.

Do I apply the changes as well?

We should probably also add something about $(...).

And I just noticed that subshells don't get highlighted.

In theory it just has to match $() but not what's inside of it, my little knowledge of regexes comes from sed, grep and awk so let's see if I can do something here lol.

edit: adding $() to -- Operators kinda of works but now the () in functions gets highlighted as well

edit2: Although comparing it to gtksourceview it really isn't that much different, when you type a ( or ) it gets highlighted automatically, the only difference is the literal $ that when typed alone doesn't get a match in gtksourceview while it would in lite-xl if I were to add $() to -- Operators

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants