How to detect multiline strings in VS Code Extension - node.js

I had been trying to match multiline string and character literals in VS Code, but there is no support for highlighting across more than one line using a regex. This is a known issue. At the bottom of the issue, it is told to use a semantic highlight provider.
VS Code's Semantic highlight guide gives a set number of tokens to semantically highlight. My main problem is that multiline strings are not detected as tokens in the first place, so they cannot be modified to change to the right color.
I am trying to match a BQN string: A BQN string is a double quote, followed by any number of non-quote characters including newlines, followed by another double quote. Double quotes inside a string are escaped by typing two quotes: "qu""ote" translates to qu"ote.
I'd like to know if there is a way to syntax highlight multiline strings via this method or any other method available to a VS Code extension. Help and examples are highly appreciated.

Related

Escape dollar sign in dollar-quoted strings query

Hello I am trying to work on a Cassandra query which has an explanation field of data type text. I am using dollar-quoted strings to escape special characters but I face problem when the string of my explanation field ends with a dollar sign
For example
INSERT INTO Users (name, explanation) VALUES ($$Tom$$, $$Some'text$$$);
The last two dollar-quoted strings are the end quotes but the third last is a part of the explanation, how can I escape that? Or is there any other way through which I can escape all special characters including dollar sign?
Thanks in advance
Looking into grammar, it looks like that the lexer is just search until the next occurrence of the $$, and doesn't distinguish between double & triple dollar signs. But if you have any character after $, then it's just handled correctly (for example, string $$dwewdewe'adqdq$'$$ works just fine) - it's only shouldn't be a last character before ending $$. If you want to insert string with ' character inside, you can just escape (see doc) it with another ' character (for example; 'this is string with '' inside' - it works fine, and will produce this is string with ' inside as expected).
This is about inserting the statement one time. But if you're inserting the data from your program, then it's better to use prepared statements instead - they should be supported in all existing drivers. When you're using prepared statements, you don't need to take care about escaping - it's the job of the driver (really, no escaping happens, as string is sent as-is, not as part of the statement). And besides the lack of need for escaping, you also should get better performance, because parsing of the statement happens only once, when it's prepared, and then only statement ID plus parameters are sent to the Cassandra node.

Substitute and change case for program variables

I'm changing some notation in a few source code files.
In particular, variable names using the format
m_variable1
m_anothervariable
should be renamed and reformatted to
mVariable1
mAnotherVariable
That is, substitute m_ with m and make the next character uppercase.
I know how todo simple substitutions, like
%s/m_/m/gc
using vim, but not sure how to add syntax for changing a char to uppercase in a substitute statement?
You can make the first character of variable name uppercase, but I think you can hardly separate words from a consecutive string simply by built-in command.
I hope following command will help you:
:%s/\vm_(\w+)/m\u\1/g
Explaination
\v enables the 'very magic' mode
\u makes the first character of word after it uppercase
\1 references the first captured group
Result
mVariable1
mAnothervariable

vim: how to let vim use apostrophe as start of a string

I am working on a sql file with some m4 macro embedded. Since m4 uses apostrophe to mark the start of string literals, it breaks the string highlighting of the file totally. Like
m4_include(`SQLCommon.m4')
I am wondering whether there is any option we can let vim to pair apostrophe with a single quotation mark. I searched on-line but didn't find any answer.
Thanks!
You're going to want to override vim's normal syntax highlighting for sqlString.
I was able to get the behavior I think you're looking for with just this:
:syntax region m4String start=/`/ end=/'/
To achieve nesting, we have to tell vim that a m4 string can contain more of the same:
:syntax region m4String start=/`/ end=/'/ contains=m4String extend
If you'd like to color those strings separately instead of just disrupting the normal highlighting you can separately link to the String highlight group.
:hi link m4String String
I wasn't entirely sure what you were looking for though; if the backtick-apostrophe delimited strings are inside of the normal SQL strings this won't work.

Issue with escape characters in Watir/Cucumber

Was hoping someone could help me with an issue I am having with escape characters in Cucumber/Watir.
I have automated tests setup. When I perform a search, 1 of the assertions I use to verify that the search has returned the correct result is to check the page for text. So my code looks like this:
Then /^I should see the following text: "([^"]*)"$/ do |str|
assert #browser.text.include?(str)
end
Here I pass in the text to search for in the string variable. e.g nike, reebok etc
So in my feature file the step is like this:
Then I should see the following text "search results for nike"
This works fine apart from 1 issue. 1 of the sites I am testing has decided to put the search term in double quotes i.e - search results for "nike"
As a result this screws up my test as I need to include the quotes as part of the search term. Therefore I need to put the word nike in escape quotes or else cucumber will recognise the first quotation around the word nike as a closing quotation. (as there is already a double quotes before it)
I have tried various different escape characters but nothing seems to work. For example I have tried the following:
\" – double quote
\\ – single backslash
Has anyone experienced similar problems and if so, how did you overcome the problem?
Thanks!
You need to change the regex rather than the string.
Problem: Your current regex says "([^"]*)", which says to match all characters between the quotations that are not quotations. This is not good given that you want to include quotations.
Solution: Change the step to the following:
Then /^I should see the following text: "(.*?)"$/ do |str|
assert #browser.text.include?(str)
end
The .* says to match all characters between the quotations. The ? makes the search lazy (instead of greedy). The ? is optional in this case, but would be important if there were additional parameters being captured. A good explanation of the greedy vs lazy can be seen at http://www.regular-expressions.info/repeat.html.

Why do I have to escape the final ]

I have a file containing string like this one :
print $hash_xml->{'div'}{'div'}{'div'}[1]...
I want to replace {'div'}{'div'}{'div'}[1] by something else.
So I tried
%s/{'div'}{'div'}{'div'}[1]/by something else/gc
The strings were not found. I though I had to escape the {,},[ and ]
Still string not found.
So I tried to search a single { and it found them.
Then I tried to search {'div'}{'div'}{'div'} and it found it again.
Then {'div'}{'div'}{'div'}[1 was still found.
To find {'div'}{'div'}{'div'}[1]
I had to use %s/{'div'}{'div'}{'div'}[1\]
Why ?
vim 7.3 on Linux
The [] are used in regular expressions to wrap a range of acceptable characters.
When both are supplied unescaped, vim is treating the search string as a regex.
So when you leave it out, or escape the final character, vim cannot interpret a single bracket in a regex context, so does a literal search (basically the best it can do given the search string).
Personally, I would escape the opening and closing square brace to ensure that the meaning is clear.
That's because the [ and ] characters are used to build the search pattern.
See :h pattern and use the help file pattern.txt to try the following experiment:
Searching for the "[9-0]" pattern (without quotes) using /[0-9] will match every digit from 0 to 9 individually (see :h \[)
Now, if you try /\[0-9] or /[0-9\] you will match the whole pattern: a zero, an hyphen and a nine inside square brackets. That's because when you escape one of [ or ] the operator [*] ceases to exist.
Using your search pattern, /{'div'}{'div'}{'div'}[1\] and /{'div'}{'div'}{'div'}\[1] should match the same pattern which is the one you want, while /{'div'}{'div'}{'div'}[1] matches the string {'div'}{'div'}{'div'}1.
In order to avoid being caught by these special characters in regular expressions, you can try using the very magic flag.
E.g.:
:%s/\V{'div'}[1]/replacement/
Notice the \V flag at the beginning of the line.
Because the square brackets mean that vim thinks you're looking for any of the characters inside. This is known as a 'character class'. By escaping either of the square brackets it lets vim know that you're looking for the literal square string ending with '[1]'.
Ideally you should write your expression as:
%s/{'div'}{'div'}{'div'}\[1\]/replacement string/
to ensure that the meaning is completely clear.

Resources