[Lua] Highlight strings used by string.{format,gsub,pack,etc}#4194
[Lua] Highlight strings used by string.{format,gsub,pack,etc}#4194mbartlett21 wants to merge 29 commits intosublimehq:masterfrom
Conversation
|
From a quick glance over this PR I noticed:
|
Noted. I'll probably look at the tests from eregex etc.
That is correct. Anchors are only matched at the ends of strings. elsewhere they match the character. |
|
It has become the practice more and more to use named contexts to |
|
I've added (roughly) the tests from the Regular Expression package and worked on getting the syntax to work with character classes and captures now.
This is fixed now so that the anchor only matches at the very start and very end. (Because of how multiline strings work, it has to account for a possible preceding newline) @michaelblyons |
|
I've added some lookahead matching on the one line for immediate calls so that it can work with |
|
This seems to be to do with Or are you meaning in the |
|
Instead of something like strings:
- match: '"'
scope: punctuation.definition.string.begin
push:
- meta_scope: string.quoted.double
- match: '"'
scope: punctuation.definition.string.end
pop: 1Use strings:
- match: '"'
scope: punctuation.definition.string.begin
push: string-double-body
string-double-body:
- meta_scope: string.quoted.double
- match: '"'
scope: punctuation.definition.string.end
pop: 1 |
- add named contexts - add `leading_wspace` var - add `first_line_language` var
Lua/Lua.sublime-syntax
Outdated
| - match: (?="(?:{{string_char_escape}}|[^\\"])*"\s*\)\s*:\s*format{{identifier_break}}) | ||
| push: fmtstr-expression | ||
| - match: (?='(?:{{string_char_escape}}|[^\\'])*'\s*\)\s*:\s*format{{identifier_break}}) | ||
| push: fmtstr-expression | ||
| - match: (?="(?:{{string_char_escape}}|[^\\"])*"\s*\)\s*:\s*(?:pack|unpack|packsize){{identifier_break}}) | ||
| push: packstr-expression | ||
| - match: (?='(?:{{string_char_escape}}|[^\\'])*'\s*\)\s*:\s*(?:pack|unpack|packsize){{identifier_break}}) | ||
| push: packstr-expression |
There was a problem hiding this comment.
Those patterns expect pack/format strings to be one-liners.
But even expressions of concatted strings (e.g.: ("one %0s two" + "another %d"):format();) are not scoped format string.
Other syntax definitions like python, don't restrict placeholder this way, as it is also valid to specify a format string in a separate variable.
fmt_str = "My %s format"
fmt_str:format("name")
I'd suggest to highlight placeholders everywhere or head for a more flexible approach using branching to scope all strings in a group followed by :format(), ... accordingly.
There was a problem hiding this comment.
It should now be working. The only thing I don't allow is having the : and format on separate lines. (which is technically valid but is a weird style)
I also updated the other expressions to allow things like string.format("%s " .. "%s") which they didn't before (only the first string was highlighted)
|
@mbartlett21 maybe you could address the change-requests? |
It now allows multiple strings to be highlighted. For example
string.match("this", "%w".."hi".."%S")
|
SublimeHQ were doing some infrastructure updates for their linux builds over the last couple of days and re-built the last dev and stable builds. I guess something broke? /cc @BenjaminSchaaf |
|
Ben said he fixed their side. |
| scope: invalid.illegal.unclosed-string.lua | ||
| pop: 2 | ||
| - match: \\. | ||
| scope: invalid.illegal.invalid-escape.lua |
There was a problem hiding this comment.
this should've been invalid.illegal.escape-character.lua
| parenthesized-expression: | ||
| - match: \( | ||
| scope: punctuation.section.group.begin.lua | ||
| branch_point: special-string-paren |
There was a problem hiding this comment.
It might make sense to name branch point the same as the context containing it.
| - parenthesized-expression-end-normal | ||
| - parenthesized-expression-inside-normal | ||
|
|
||
| # at this point the stack is: |
There was a problem hiding this comment.
Maybe remove these comments as well as the "pop this as well as ..." ones below? They were mainly for yourself while you were working on this. We don't need them.
| pattern-unquoted-charclass: | ||
| - meta_include_prototype: false | ||
| - include: pattern-shared-charclass | ||
| - match: \%b.?.? |
There was a problem hiding this comment.
\%b.{0,2} might be more explicit
|
I didn't have a lot of time to look through this.
|
This PR adds highlighting to the strings that are passed to
string.{format,find,match,gsub,gmatch,pack,unpack,packsize}.They are split into three categories:
Format strings
https://www.lua.org/manual/5.4/manual.html#pdf-string.format
These are done similarly to C's
sprintfand friends, except that Luaonly supports some specifiers and adds
qas an option.Patterns
https://www.lua.org/manual/5.4/manual.html#6.4.1
They are passed as the second argument to the four functions that take
patterns, meaning that I can also match on
(whatever_value):match 'this%s*that'. For scopes, I have roughlyfollowed what the builtin regular expressions package does.
I haven't worked on nested groups as that got quite hard with handling
it inside the three different types of strings that Lua provides.
The special capture
()returns a position. I'm not sure what scopethis should be. I've set it to
punctuation.section.brackets.luaforthe moment.
Pack format strings
https://www.lua.org/manual/5.4/manual.html#6.4.2
The alignment and endian specifiers I have scoped as
storage.modifierand I have scoped the actual type designations asstorage.type. For the padding that can be specified withxorX,I've scoped it to
punctuation.separator.paddingas I didn't see anybetter one, and that helped differentiate it from ones that have
values.
Since format strings are always the first argument, I did not find a wayof handling them when the function is called as a method.
(e.g.
('%3.5f'):format(3)).For format strings and pack strings, they now use branching inside
parenthesized-expressionto choose what gets formatted.