Is there any way in VSCode to show/highlight characters passing a character limit for a line? - visual-studio-code

Is there any way in VSCode to show/highlight characters passing a character limit for a line such as 80 (possibly ignoring whitespace in the left side)? I sometimes use Vim, in Vim, there is a script for that. I have searched the VSCode Marketplace and have found nothing. I like to keep my code consistent.

The Highlight extension can do this. In your settings:
"highlight.regexes": {
"(\\s*)(.{80})(.*)": { // skip leading whitespace, skip next 80 characters
// "filterLanguageRegex": "javascriptreact",
"decorations": [
{}, // do nothing to first 2 capture groups
{},
{
// "outline": "green solid 2px",
"borderWidth": "0 0 2px 0",
"borderColor": "red",
"borderStyle": "solid"
},
]
}
},
For styling options see https://code.visualstudio.com/api/references/vscode-api#DecorationRenderOptions

This extension seems to do what you're looking for, however it doesn't seem to be very popular yet:
"codewall" on VS Code MarketPlace
GitHub Repository

Related

vscode surround text with snippet with correct indentation

I am trying to create a snippet that surrounds the selected text with try..except block. This is what I have in keybindings.json file:
{
"key": "ctrl+p'",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection",
"args": {
"snippet": "try:\r\n\t${TM_SELECTED_TEXT}\r\nexcept BaseException as ex:"
}
}
This works for most part except that if I select the entire line of indented code, it inserts try at the beginning of the line. I want it to behave like Command+/ which adds # right before where the text starts.
How do I make my snippet behave like that?
You have to insert a possible whitespace in front of each line and remove the whitespace on the middle line:
{
"key": "ctrl+p'",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection",
"args": {
"snippet": "${TM_SELECTED_TEXT/^([ \\t]*).*$/$1/}try:\r\n${TM_SELECTED_TEXT/^([ \\t]*).*$/$1/}\t${TM_SELECTED_TEXT/^[ \\t]*(.*)$/$1/}\r\n${TM_SELECTED_TEXT/^([ \\t]*).*$/$1/}except BaseException as ex:"
}
}
Dang, it took a while but here is something pretty simple that I think works.
First, make this snippet (in some snippets file):
"try except": {
// "scope": "python",
// "prefix": "tryWrap", // if you want it
"body": [
"${1:${TM_SELECTED_TEXT/^([ \\t]*)[\\s\\S]*$/$1/}}try:",
"${TM_SELECTED_TEXT/^(.*?)$(\\r?\\n)?/\t$1$2/gm}", // note the 'g' flag !!!
"$1except BaseException as ex:"
]
}
and then this keybinding (in your keybindings.json):
{
"key": "alt+q",
"command": "editor.action.insertSnippet",
"args": {
"name": "try except",
}
}
That middle line of the snippet:
"${TM_SELECTED_TEXT/^(.*?)$(\\r?\\n)?/\t$1$2/gm}"
will actually run once for each line of your selection, because the match is from ^ to the end of the same line and because of the global g flag. So it'll keep running as long as it finds matches in your entire selection.
The leading whitespace is computed by ${1:${TM_SELECTED_TEXT/^([ \\t]*)[\\s\\S]*$/$1/}} which will be the first line of your selection. It isn't computed for each line (although it probably could be, it would just be unnecssarily messy). So don't select part of the leading white space of that first line of the selection - actually it seems to work fine as long as select whole tabs worth of that leading white space, just not an extra space. It is easy to do it right.
#rioV8's snippet works for me (for single lines only it seems) but it could be simplified a bit. I upvoted it.
Note that 3 parts of the snippet are identical: ${TM_SELECTED_TEXT/^([ \\t]*).*$/$1/}
So to simplify that resulting value (the whitespace before the selected text) can be stored in a value and reused. See this:
${1:${TM_SELECTED_TEXT/^([ \\t]*).*$/$1/}} // wrapped in tabstop 1
Now you can just use $1 in any other place you want that same value.
"snippet": "${1:${TM_SELECTED_TEXT/^([ \\t]*).*$/$1/}}try:\r\n$1\t${TM_SELECTED_TEXT/^[ \\t]*(.*)$/$1/}\r\n$1except BaseException as ex:"
See there are two $1's not part of a transform, like try:\r\n$1\t : that $1 will be your computed whitespace from ${1:${TM_SELECTED_TEXT/^([ \\t]*).*$/$1/}}
Also, this part: ${TM_SELECTED_TEXT/^[ \\t]*(.*)$/$1/} can be simplified to
${TM_SELECTED_TEXT/^[ \\t]*//}
which matches the leading white space before the text and replaces that white space with nothing.
Result:
"snippet": "${1:${TM_SELECTED_TEXT/^([ \\t]*).*$/$1/}}try:\r\n$1\t${TM_SELECTED_TEXT/^[ \\t]*//}\r\n$1except BaseException as ex:"
This is just a little cleaner and less prone to typos. You just have to do one tab or escape at the end to finish.

Disable VSCode ESLint warnings, but keep formatting functionality

I am using VSCode's ESLint extension to fix and format my TS code. I would like to remove all the warnings (such as red underlines) from ESLint, because I find them really annoying and distracting.
Using the config
"eslint.enable": true,
removes all warnings, but also disables the formatting.
In VS Code settings you can define an entry called "eslint.rules.customizations" and provide an array of rules with a severity setting set to "off".
{
"eslint.rules.customizations": [
{ "rule": "<rule-name>", "severity": "off" }
],
}
For example, if you are trying to remove the red (error) or yellow (warn) squigglies for the rules "react/jsx-curly-brace-presence" and "prettier/prettier" you can configure the following:
{
"eslint.rules.customizations": [
{ "rule": "prettier/prettier", "severity": "off" },
{ "rule": "react/jsx-curly-brace-presence", "severity": "off" }
]
}

How can I underline whitespaces in VS Code using textMateRules?

I am using VS Code and would like to have markdown headings displayed underlined in the source code. I have added the following configuration:
"editor.tokenColorCustomizations": {
"textMateRules": [
{
"scope": [
"markup.heading.markdown",
],
"settings": {
"foreground": "#C0C3CA",
"fontStyle": "underline",
}
},
],
}
This basically works. The spaces in between the words however are not underlined, see this screenshot:
Is there a possibility to have these underlined as well?
According to the scope inspector the spaces also have the markup.heading.markdown scope, just like the words. So I don't see why they do not get underlined.
Any ideas?
It conflicts with "editor.renderWhitespace": "all" setting.
You can find out more a about why it happens on GitHub:
https://github.com/microsoft/vscode/issues/49462
https://github.com/eclipse/che-che4z-lsp-for-hlasm/issues/6

How to insert or realign a line comment at a predefined column in VSCode

Is there a way to insert or realign comments in vscode like in emacs (i.e. M-;). In emacs a meta-; would insert a new comment at a predefined comment column if there isn't already a comment or realign the comment to the comment column if there is. This would be a nice feature. For instance 'cmd+;' would insert '// ' in javascript code. If there is an extension or keyboard shortcut definition that would do this I'd appreciate hearing about it.
I think I have this working in a macro. You will need the macrosRE extension.
In your settings.json:
"macros": {
"commentTabStop": [
"editor.action.commentLine",
// go to beginning of any text on line
"cursorHome",
// now select all whitespace at beginning of line
{
"command": "cursorMove",
"args": {
"to": "wrappedLineStart",
"select": true
}
},
// set your number of tab stops to place comment
"tab","tab","tab","tab","tab","tab"
]
}
That is longer than I would have hoped but to handle creating a comment on existing text that may or may not have whitespace at the beginning. I am assuming you want all comments to be vertically aligned no matter the amount of leading tabs/space that may have had originally. And to work when creating comments on empty lines.
function fooBar() {
const someVar;
}
becomes
// function fooBar() {
// const someVar;
// }
Unfortunately, the internal indentation within the function is lost. But you could just reformat that part if you uncommented the code later. Select it and Ctrl-K Ctrl-F will fix the internal indentation.
To make the keybinding put this into your keybindings.json:
{
"key": "ctrl+;",
"command": "macros.commentTabStop"
},

Sublime Text autocomplete window closes when scrolling off the list

When scrolling the autocomplete list with up or down, if you go too far in either direction (e.g. there are no more suggestions), the list will close.
The behavior I want is to have the list wrap when reaching the end instead of close.
This is easy to fix with downward scrolling by assigning this hotkey:
{ "keys": ["down"], "command": "auto_complete", "context":
[ { "key": "auto_complete_visible" } ]
},
That's because the auto_complete command has built-in functionality to scroll downward each time it's invoked, which is why the hotkey works.
...But upward scrolling is different. I've tried about 20 different hotkey and macro combinations with no success.
I'm almost certain the only way to achieve this behavior is with a plugin, but unfortunately my Python skill level is nil.
If it matters, I'm manually invoking autocomplete with ctrl+space (the automatic popup is disabled).
I'm using Sublime Text 2.
Best solution: use auto_complete_cycle settings (added 26 March 2015):
Please use this new simple solution and not the python plugin
Sublime Text new version relased on 24 March 2015 has a new setting called auto_complete_cycle that implement this behaviour. Set it to true to iterate through the autocomplete results.
"auto_complete_cycle": true
Worst old solution: this custom plugin
I have just made this plugin that works well on Sublime Text 3 in Linux Mint. I have not tested it in Sublime Text 2 but think the plugin system it's the same, so, it should work on that version too. The workaround used it's not too pretty but works.
import sublime, sublime_plugin
class UpArrowInAutoCompleteCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.settings().set('autoCompleteFlag',True)
self.view.settings().set('initialPoint', self.view.sel()[0].begin())
""" Move one line up """
self.view.run_command('move', {"by": "lines", "forward": False});
""" Auto-complete was opened and up arrow was pressed, so if the cursor changes
(on_selection_modified will be triggered) we have gone outside the list.
If we were not in the first element on_selection_modified will not be triggered, so
we turn of the flag"""
sublime.set_timeout(lambda: self.view.settings().set('autoCompleteFlag', False),300)
class AutoCompleteSelectionModifiedTriggerCommand(sublime_plugin.EventListener):
def on_selection_modified(self, view):
if view.settings().get('autoCompleteFlag'):
""" If the up arrow was pressed and on_selection_modified
has been triggered, then we know that we were in the first element
of the list and we hitted the up arrow"""
view.settings().set('autoCompleteFlag', False)
initialPoint = view.settings().get('initialPoint')
""" We don't know how many words the auto_complete has, so,
in order to calculate that number, we move down in the list
till we get outside the list. After that we make the list appear
again and move down n-1 times to go (and stay) to the last line """
view.sel().clear()
view.sel().add(initialPoint)
view.run_command('auto_complete')
numLines = 0
while view.sel()[0].begin() == initialPoint:
view.run_command('move', {"by": "lines", "forward": True})
numLines += 1
if numLines == 401:
return
if numLines == 0:
return
view.sel().clear()
view.sel().add(initialPoint)
view.run_command('auto_complete')
numLine = 0
while numLine < (numLines-1):
view.run_command('move', {"by": "lines", "forward": True})
numLine += 1
To make the plugin use Tools>new Plugin and paste the code. Then save it in Packages/User folder. You can use Preferences>Browse Packages to find the Packages floder, inside which the User folder is located.
To make it work I added to my user key-bindings file this bindings (the second it's your own binding):
{
"keys": ["up"],
"command": "up_arrow_in_auto_complete",
"context": [{
"key": "auto_complete_visible",
"operator": "equal",
"operand": true
}]
}, {
"keys": ["down"],
"command": "auto_complete",
"context": [{
"key": "auto_complete_visible"
}]
}
Edit: this is an example result: