Regex matching but no highlighting - visual-studio-code

I am currently creating a language extension. When adding comments (starts with $$), I ran into a hurdle.
"comments":{
"patterns": [{
"name": "comment.line.sahutorepol",
"match": "\\$\\$.*$\\n?"
}]
},
Despite the regex matching and being escaped properly, there is no highlighting and the inspect tool confirms that its not properly detected.
I have tried placing the match key outside the patterns array, to no change.

Related

Change text color for specific lines based on pattern matching in VS Code

I'm writing some notes in a markdown file in VS Code and would like to see certain lines of text have their text color changed (not highlighted or background color changed).
Example:
Desired result (by example of opening the same file in gedit):
I would like to know if there's a plugin or customization option that fulfills my requirement.
Additionally and if possible I'd like the solution to fulfill some optional requirements:
customize the color that is applied based on set patterns:
eg. apply green color to the entire line when pattern (\t|\s)*✔.* has been matched
in my specific case, I'd like to apply 4 different colors based on 4 different symbols or set of characters that should be matched: ✔, -->, ⤵ and ✘
only apply color changes to lines based on specified file extensions:
eg. .* match all file extensions; .md, .cs, .py match only markdown, C# and Python files
in my specific case, I'd like to only match on Markdown files with the .md extension
A great plugin that does something similar is: https://github.com/aaron-bond/better-comments
Unfortunately this one only matches code tags within comments, and I'd like to match outside of comments as well.
Related threads: VS 2010: Change color of lines based on a pattern
Installed Highlight as suggested by riov8 and Mark.
I've made some changes to my configuration compared to the initial specification in the question.
This is the configuration I'm using that meets the requirements of the question and then some:
"highlight.regexes": {
"((\\s+✔.*)|(✔\\s+.*))": {
"regexFlags": "gi",
"filterLanguageRegex": "markdown",
"filterFileRegex": ".*\\.md",
"decorations": [{"color": "#679142"}]
},
"((\\s+-->.*)|(-->\\s+.*))": {
"regexFlags": "gi",
"filterLanguageRegex": "markdown",
"filterFileRegex": ".*\\.md",
"decorations": [{"color": "#FF8C00"}]
},
"((\\s+⤵.*)|(⤵\\s+.*))": {
"regexFlags": "gi",
"filterLanguageRegex": "markdown",
"filterFileRegex": ".*\\.md",
"decorations": [{"color": "#569CD6"}]
},
"((\\s+✘.*)|(✘\\s+.*))": {
"regexFlags": "gi",
"filterLanguageRegex": "markdown",
"filterFileRegex": ".*\\.md",
"decorations": [{"color": "#FF6464"}]
},
NOTE: There was an error in earlier versions of this configuration where matching on any whitespace \\s did not work correctly. Read comments for further elaboration on this issue.
Here's how the color highlighting is now applied to my markdown file:

Visual Studio Code - Change the value of a specific integer with a math operation in a line across multiple pages

Not sure if this is something that can be done through Visual Studio Code, but I thought I would check here before I go ahead and manually modify thousands of lines in my project.
Please see the example below to help explain the issue I'm facing.
I have multiple files spread across multiple folders with an identifying text (in this case, Bolts), with another identifier (in this case, quality) value which equals an integer that is different on each page. I would like to know if it's possible to replace these integers with the updated integers through a basic mathematical equation.
Page A:
{
"key": "storage01/bolts",
"quality": 53.12,
"weight": 30
},
{
"key": "storage01/cogs",
"quality": 39.17,
"weight": 29
}
Page B:
{
"key": "storage02/bolts",
"quality": 18.9,
"weight": 30
},
{
"key": "storage02/cogs",
"quality": 76.2,
"weight": 29
}
I would like to change both numbers of the quality by multiplying them by 0.29 so the results would end up like this ..
Page A:
{
"key": "storage01/bolts",
"quality": 15.4048,
"weight": 30
},
{
"key": "storage01/cogs",
"quality": 39.17,
"weight": 29
}
Page B:
{
"key": "storage02/bolts",
"quality": 5.481,
"weight": 30
},
{
"key": "storage02/cogs",
"quality": 76.2,
"weight": 29
}
With the help of map-replace.js and one other extension Search Editor: Apply Changes you can do this pretty easily. Thanks to #rioV8 for the first extension, its replacement code and the second regex.
The idea is to do a search across files in a Search Editor, map-replace those values in that editor and then save those changes to all the underlying files.
(1) Open a new Search Editor (from command palette or make a keybinding to the command) or do a Search in the Panel and then choose Open in Editor.
(2) Do a regex search for \{\s*.*bolts[\w\W]*?\}
(3) Now in the Search Editor tab do a Find with (?<="quality":\s*)[\d.]+
(3) Alt+Enter to select all the matches (you may have to click in the editor first).
(4) Run Map and Replace Selection with JavaScript Function with (v, i) => `${(Number(v)*0.29).toFixed(2)} to make the desired changes. Set toFixed() to what you want.
(5) Run Apply Search Editor changes to worksapce from Search Editor: Apply Changes extension
and your changes will be saved within all the files. Pretty neat to use these extensions and the Search Editor functionality together - yours is a good use case (as long as the initial regex across files isn't super difficult to get right).
(6) Save all
Demo (with simplified replace since I can't seem to paste into the input box):
You can use the extension map-replace-js
You select the numbers with a regex (?<="quality": )[\d.]+
Use ShiftCtrl+L to select all occurrences in the file.
Then call the Map and Replace Selection with JavaScript Function command and use the following expression to convert the numbers
(v, i) => `${(Number(v)*0.29).toFixed(2)}`
You have to do this in every file

Regex in VSCode: case conversion in replace, modifiers \l, \L, \U, \u not working

I'm trying to replace a structure like this:
testVars.bread.componentFlour
testVars.bread.componentWater
to something like this:
testVars.dough.flour
testVars.dough.water
this happens with multiple variable names; I want to remove the component and have the first letter converted to lowercase to match CamelCase.
What I tried doing was matching testVars.bread.component(.) replacing it with testVars.dough.\l$1.
According to regex documentation, that should convert my match to lowercase. However, VSCode wants to insert \l as text.
How do I get VSCode to convert my match to lowercase?
EDIT: To clarify, this is strictly for VSCode's implementation, not a regex question itself. Matching this group in notepad++ and replacing with testVars.dough.\l\1 does exactly what I want it to do.
NEW ANSWER: !! Those case modifiers like \l and \u are being added to the find/replace functionality in vscode (both in find within one file and search across multiple files [v1.49] - see https://github.com/microsoft/vscode/pull/105101) (see https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_47.md#case-changing-in-regex-replace) - will be in v1.47 (find) and v1.49 (search across files).
So that your original thought to
Find : testVars.bread.component(.)
Replace : testVars.dough.\l$1
is now working.
For more on how the case modifiers work in vscode, see https://stackoverflow.com/a/62270300/836330
OLD ANSWER:
While you cannot replace with a lowercase identifier in vscode, you still have some of options.
Use a regex like (?<=testVars\.).*\.component(.*) so that testVars. is not captured - because you do not want to change its case.
Ctrl+Shift+L to select all your matches (same aseditor.action.selectHighlights).
Ctrl+Shift+P, type lowerand trigger that command (or make a keybinding for this unbound command).
Trigger your replaceAll
To speed that up you have two options. (1) Make a macro that would run steps 2, 3 and 4. Or (2) make a snippet that will transform your selections - effectively running steps 3 and 4 at once.
Snippet in your keybindings.json (not in a snippets file):
{
"key": "alt+m", // choose some keybinding
"command": "editor.action.insertSnippet",
"args": {
"snippet": "${TM_SELECTED_TEXT/.*\\.component(.*)/dough.${1:/downcase}/}"
},
},
and then Ctrl+Shift+L to select all, and alt+m to trigger the above insertSnippet.
Macro approach:
Using some macro extension, here multi-command, put this into your settings.json:
"multiCommand.commands": [
{
"command": "multiCommand.findReplaceLowercase",
"sequence": [
"editor.action.selectHighlights", {
"command": "editor.action.insertSnippet",
"args": {
"name": "replace and lowercase",
}
},
]
}
]
and a snippet, in one of your snippets files:
"replace and lowercase": { // this "label" is used in the macro
"prefix": "for",
"body": [
"${TM_SELECTED_TEXT/.*\\.component(.*)/dough.${1:/downcase}/}"
// "${TM_SELECTED_TEXT/(.*)/${1:/downcase}/}" // to downcase all selected text
],
"description": "replace selected text"
},
and a keybinding to trigger the macro:
{
"key": "alt+m", // choose some keybinding
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.findReplaceLowercase" },
}
In the demo I copy the find regex into the find widget (or just write it there, it doesn't matter how it gets there or where focus is) and then hit alt+m (the macro keybinding) and that is it.
Obviously, this looks like a lot of work but you could keep reusing the macro and snippet, transforming to the replace result you would like the next time. And there you can use /downcase, /upcase, /capitalize and /pascalcase all you want which you can't use in the replace field of the find/search widgets.

Create a Sublime Text 3 .sublime-completions autocomplete file on mac

The Sublime Text documentation is clear on the syntax for HTML, but not clear on where to place the file for mac using version 3. I want to generate my own auto-completes for Plain Text. Ideally allowing me to down/up arrow through a list of likely auto-completes.
{
"scope": "text.html - source - meta.tag, punctuation.definition.tag.begin",
"completions":
[
{ "trigger": "a", "contents": "$0" },
{ "trigger": "abbr\t<abbr>", "contents": "<abbr>$0</abbr>" },
{ "trigger": "acronym", "contents": "<acronym>$0</acronym>" }
]
}
All package resource files need to be stored in a Package for Sublime to be able to find and load it. For your own customizations, the appropriate place to place the file is in your User package, which you can find via Preferences > Browse Packages from the menu.On MacOS, that would be Sublime Text > Preferences > Browse Packages.
In order to have completions for plain text, you need to change the scope from the HTML specific scope to one for plain text.
In order to determine the scope that you want to apply (which also counts for things like Key Bindings, Snippets, Build Systems, and so on) you can select Tools > Developer > Show Scope Name from the menu (see the menu for the key binding assigned to this) to see what the full scope is for the current cursor location.
As evidenced from the scope you mentioned in your question, scopes can be quite complex to allow you to dial in as much specificity as you want.
For the case of simple plain text, as evidenced by the command I mentioned above, the following is the example completions set to work in plain text:
{
"scope": "text.plain",
"completions":
[
{ "trigger": "a", "contents": "$0" },
{ "trigger": "abbr\t<abbr>", "contents": "<abbr>$0</abbr>" },
{ "trigger": "acronym", "contents": "<acronym>$0</acronym>" }
]
}
Note that along with the location of the file, the extension is also important, otherwise Sublime won't know what it's supposed to contain.

Sublime Text 2: Which scope is used for autocomplete?

I use an include rule in Markdown.tmLanguage to get some LaTeX syntax highlighting for LaTeX environments in Markdown files.
"tex_environments": {
"name": "markup.raw.inline.markdown",
"comment": "TeX Environments",
"begin": "\\\\begin\\{[^\\}]*\\}",
"end": "\\\\end\\{[^\\}]*\\}",
"patterns": [
{ "include": "text.tex.latex" }
]
}
Inside these environments I still get HTML autocompletion. The Scope Hunter plugin shows the following scopes:
text.html.markdown
meta.paragraph.markdown
markup.raw.inline.markdown
I assume that the first scope determines the language for autocompletion? Can I convince Sublime that I want LaTeX autocompletion?