-
Notifications
You must be signed in to change notification settings - Fork 598
[Lua] Highlight strings used by string.{format,gsub,pack,etc} #4194
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
base: master
Are you sure you want to change the base?
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 |
Lua/Lua.sublime-syntax
Outdated
| push: | ||
| - pattern-unquoted-balanced-char | ||
| - pattern-unquoted-balanced-char | ||
| - match: \% |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume:
- match: '%.'
scope: constant.character.escape.luashould work just fine? No?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure. With the quoted ones, it needs to be in a separate pattern so that the additional prototype can end the string if it is %', but I'll test what happens with an embedded syntax
|
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
|
added some named contexts and section markers |
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
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.