Is there a NOT operator in VSCode Keybindings JSON to invert an entire "when" clause? - json

The "when" clause in VSCode's JSON keybindings editor is very useful, but I can't seem to find a way to reverse it. For instance, if I want an action to only work when the Terminal view is not showing, I would want something like this:
"when": "not.terminalViewShowing"
or
"when.not": "terminalViewShowing"
Is something like this possible and if not, are there any plans to add it?

If the when clause only contains one condition, then you can just invert that single condition with the ! operator. For the current list of operators in when-clauses, see the official docs: https://code.visualstudio.com/api/references/when-clause-contexts#conditional-operators.
If the when clause contains multiple condition joined by logical operators...
See this GitHub issue: Add support for parenthesis in "when" conditions #91473, which- once implemented- would allow you to do "when": "!(...)". You can show your support for the feature request by giving a thumbs up reaction to the issue. But please don't make a "me too" comment. "me too" comments generally come off as annoying to repo maintainers because they clutter up discussion and don't contribute anything of significant value.
As a current workaround, you could use De Morgan's laws.

It is as simple as adding the ! before your terminalViewShowing.
"when": "!terminalViewShowing"
BYW, parentheses are not supported in when clauses, see Add support for parenthesis in "when" conditionshttps://github.com/microsoft/vscode/issues/91473 and https://github.com/microsoft/vscode/issues/147904.
Update: A PR has just been merged that introduces a new parser for when clauses which does, finally, support parentheses, see context keys: implement a new parser (and a scanner/lexer) for 'when' clauses.

Related

Filter the Chrome console messages

Is there a way to black filter the messages in the Chrome console?
By example, I don't want to see messages from/containing the JQMIGRATE...
You can negate filters by prepending with -. For example, -JQMIGRATE will exclude messages containing the string "JQMIGRATE".
Regex filters can also be negated this way. e.g. -/^DevTools/ will exclude messages that begin with "DevTools".
(Credit goes to Donald Duck, who suggested this in a comment on the chosen answer. I thought it was a far cleaner solution than negative lookaheads, and deserved to be elevated to a top-level answer.)
I think you can. Recent chrome versions allow filtering using regular expressions by enclosing the search pattern in /.../, previous versions had a checkbox. You can express, for instance, "not bar" by using negative lookaheads, as described here How to negate specific word in regex?
In the picture below, I tried filtering for "not bar".
I've found this to simple filter all console messages from extensions. This will also persist until you clear the filter. Type -chrome-extension:. This will REMOVE any messages that come from -> chrome-extension: <- urls.
You can use multiple filters by putting a space between each query. Ex. -chrome-extension: -cookie. Notice the space after the : and before the -c. This will REMOVE any messages that come from -> chrome-extension: <- urls and also REMOVE any messages with cookie in the context.
Using Chrome
Version 80.0.3987.132 (Official Build) (64-bit)
The accepted answer works for single line filtering, but I found it also filters out any multi-line logs.
See the following examples.
No filter:
Accepted answer (missing multi-line logs):
Fixed negative filter (correctly keeps multi-line logs):
/^((.|\s)(?!Violation))+$/
Bonus! Easy filtering out multiple matches:
/^((.|\s)(?!Violation|creating))+$/

How would you define a keyword beyond the fact that it is reserved.

My question is Python specific (3.4.3).
My question is specific to Built-In functions only.
It is clear to me the difference between a keyword (reserved word) and an identifier (user-defined variable).
See: https://en.wikipedia.org/wiki/Reserved_word
Likewise, I understand the basic meaning of the terminology 'function'
See : https://en.wikipedia.org/wiki/Functional_programming \
and
http://www.learnpython.org/en/Functions
However, I am having difficulty understanding the difference between Built-in functions and keywords; such as 'if' and 'for'.
https://docs.python.org/3/library/functions.html#built-in-funcs
What is the difference between the two? Keyword and Function.
Is the Keyword 'if' not simply a built in function? If so, why does it not appear in the official list of Built-In functions in the Python documentation?
https://docs.python.org/3/library/functions.html#built-in-funcs
It certainly behaves as a function. Is it simply because it preforms a procedure as opposed to returning a value? In which case how would you define it? As a method?
I have searched high and low on stackoverflow and I cannot seem to locate an answer.
Answers such as the two examples given below do not answer the overriding questions for me. Which are;
1) What defines a keyword as a keyword, rather than a builtIn function?
2) If keywords such as 'if' are not functions, then what are they? They are not classes etc. I understand that 'IF' is an example of a condition statement but what is the generic terminology for these keywords. The word keyword only defines the fact that it is reserved within the language, it does not define what the actual object is, i.e. function, class, method etc.
http://stackoverflow.com/questions/6054672/whats-the-difference-between-a-keyword-or-a-statement-and-a-function-call
http://stackoverflow.com/questions/155609/difference-between-a-method-and-a-function?rq=1
Keywords are those that describe the action to be performed, or specify how to interpret something (give meaning to instructions)
Functions are simply labels (for a set of instructions).
If you change function names it won't matter to Python (you can edit built in modules), but you can't relabel keywords.
You have already added tons of references to both, so I will not cite more.

Matching nested constructs in TextMate / Sublime Text / Atom language grammars

While writing a grammar for Github for syntax highlighting programs written in the Racket language, I have stumbled upon a problem.
In Racket #| starts a multiline comment and |# ends it.
The problem is that multiline comments can be nested:
#| a comment #| still a comment |# even
more comment |#
Here is my non-working attempt:
repository:
multilinecomment:
begin: \#\|
end: \|\#
name: comment
contentName: comment
patterns:
- include: "#multilinecomment"
name: comment
- match: ([^\|]|\|(?=[^#]))*
name: comment
The intent of the match patterns are:
"#multilinecomment"
A multiline comment can contain another multiline comment.
([^\|]|\|(?=[^#]))*
The meaning of the subexpressions:
[^\|] any characters not an `|`
\|(?=[^#]) an `|` followed by a non-`#`
The entire expression thus matches a string not containg |#
Update:
Got an answer from Allan Odgaard on the TextMate mailing list:
http://textmate.1073791.n5.nabble.com/TextMate-grammars-and-nested-multiline-comments-td28743.html
So I've tested a bunch of languages in Sublime that have multiline comments (C/C++, Java, HTML, PHP, JavaScript), and none of the language syntaxes support multiline comments embedded in multiline comments - the syntax highlighting for the comment scope ends with the first "comment close" marker, not with symmetric markers. Now, this isn't to say that it's impossible, because the BracketHighlighter plugin works great for matching symmetric tags, brackets, and other markers. However, it's written in Python, and uses custom logic for its matching algorithms, something that may not be available in the Oniguruma engine that powers Sublime's syntax highlighter, and apparently Github's as well.
Basically, from your description of the problem, you need a code parser to ensure that nested comments are legal, something you can't do with just a syntax highlighting definition. If you're writing this just for Sublime, a custom plugin could take care of that, but I don't know enough about Github's Linguist syntax highlighting system to say if you're allowed to do that. I'm not a regex master yet, but it seems to me that it would be rather difficult to achieve this purely by regex, as you'd need to somehow keep track of an arbitrary number of internal symmetric "open" and "close" markers before finding (and identifying!) the final one.
Sorry I couldn't provide a definitive answer other than I'm not sure this is possible, but that's the best I can come up with without knowing more about Sublime's and Github's internals, something that (at least in Sublime's case) won't happen unless it's open-sourced. Good luck!
Old post, and I don't have the reputation for a comment, but it is emphatically NOT possible to detect arbitrarily nested comments using purely regular expressions. Intuitively, this is because all regular expressions can be transformed into a finite state machine, and keeping track of nesting depth requires a (theoretically) infinite amount of state (the number of states needs to be equal to at least the different possible nesting depths, which here is infinite).
In practice this number grows very slowly, so if you don't want to go to too much trouble you could probably write something that allows nesting up to a reasonable depth. Otherwise you'll probably need a separate phase that parses through and finds the comments to tell the syntax highlighter to ignore them.
You had the correct idea but it looks like your second pattern also matches for the "begin nested comment" sequence #| which will never give a chance for your recursive #multilinecomment pattern to kick in.
All you have to do is replace your second pattern with something similar to
(#(?=[^|])|\|(?=[^#])|[^|#])+
Take the last match out. You do not need it. Its redundant to what textmate will do naturally, which is to match all additional text in to the comment scope until the end marker comes along, or the entire pattern recurses upon itself.

Proper function prefix notation

Not sure if this is the right place to post this question but:
If I have a list of functions that I am listing in a spec doc, lets say
MyObj_Func1
MyObj_Func2
MyObj_Func3
and so on but I just want to list the core name of the functions (Func1, Func2, Func3) How do I note at the top to say "the functions listed below all start with "MyObj_"?
Something like: MyObj_:: and then ... before the function names in the list?
I would leave the prefixes in the documentation. Though repetitive, it's what I would expect to see (as a c developer).
For example see the GNOME docs for hash table (which I consider to be fairly well done): https://developer.gnome.org/glib/2.31/glib-Hash-Tables.html.

should I write more descriptive function names or add comments?

This is a language agnostic question, but I'm wandering what people prefer in terms of readability and maintainability... My hypothetical situation is that I'm writing a function which given a sequence will return a copy with all duplicate element removed and the order reversed.
/*
*This is an extremely well written function to return a sequence containing
*all the unique elements of OriginalSequence with their order reversed
*/
ReturnSequence SequenceFunction(OriginalSequence)
{...}
OR
UniqueAndReversedSequence MakeSequenceUniqueAndReversed(OriginalSequence)
{....}
The above is supposed to be a lucid example of using comments in the first instance or using very verbose function names in the second to describe the actions of the function.
Cheers,
Richard
I prefer the verbose function name as it make the call-site more readable. Of course, some function names (like your example) can get really long.
Perhaps a better name for your example function would be ReverseAndDedupe. Uh oh, now it is a little more clear that we have a function with two responsibilities*. Perhaps it would be even better to split this out into two functions: Reverse and Dedupe.
Now the call-site becomes even more readable:
Reverse(Dedupe(someSequence))
*Note: My rule of thumb is that any function that contains "and" in the name has too many responsibilities and needs to be split up in to separate functions.
Personally I prefer the second way - it's easy to see from the function name what it does - and because the code inside the function is well written anyway it'll be easy to work out exactly what happens inside it.
The problem I find with comments is they very quickly go out of date - there's no compile time check to ensure your comment is correct!
Also, you don't get access to the comment in the places where the function is actually called.
Very much a subjective question though!
Ideally you would do a combination of the two. Try to keep your method names concise but descriptive enough to get a good idea of what it's going to do. If there is any possibility of lack of clarity in the method name, you should have comments to assist the reader in the logic.
Even with descriptive names you should still be concise. I think what you have in the example is overkill. I would have written
UniqueSequence Reverse(Sequence)
I comment where there's an explanation in order that a descriptive name cannot adequately convey. If there's a peculiarity with a library that forced me to do something that appears non-standard or value in dropping a comment inline, I'll do that but otherwise I rely upon well-named methods and don't comment things a lot - except while I'm writing the code, and those are for myself. They get removed when it is done, typically.
Generally speaking, function header comments are just more lines to maintain and require the reader to look at both the comment and the code and then decide which is correct if they aren't in correspondence. Obviously the truth is always in the code. The comment may say X but comments don't compile to machine code (typically) so...
Comment when necessary and make a habit of naming things well. That's what I do.
I'd probably do one of these:
Call it ReverseAndDedupe (or DedupeAndReverse, depending which one it is -- I'd expect Dedupe alone to keep the first occurrence and discard later ones, so the two operations do not commute). All functions make some postcondition true, so Make can certainly go in order to shorten a too-long name. Functions don't generally need to be named for the types they operate on, and if they are then it should be in a consistent format. So Sequence can probably be removed from your proposed name too, or if it can't then I'd probably call it Sequence_ReverseAndDedupe.
Not create this function at all, make sure that callers can either do Reverse(Dedupe(x)) or Dedupe(Reverse(x)), depending which they actually want. It's no more code for them to write, so only an issue of whether there's some cunning optimization that only applies when you do both at once. Avoiding an intermediate copy might qualify there, but the general point is that if you can't name your function concisely, make sure there's a good reason why it's doing so many different things.
Call it ReversedAndDeduped if it returns a copy of the original sequence - this is a trick I picked up from Python, where l.sort() sorts the list l in place, and sorted(l) doesn't modify a list l at all.
Give it a name specific to the domain it's used in, rather than trying to make it so generic. Why am I deduping and reversing this list? There might be some term of art that means a list in that state, or some function which can only be performed on such a list. So I could call it 'Renuberate' (because a reversed, deduped list is known as a list "in Renuberated form", or 'MakeFrobbable' (because Frobbing requires this format).
I'd also comment it (or much better, document it), to explain what type of deduping it guarantees (if any - perhaps the implementation is left free to remove whichever dupes it likes so long as it gets them all).
I wouldn't comment it "extremely well written", although I might comment "highly optimized" to mean "this code is really hard to work with, but goes like the clappers, please don't touch it without running all the performance tests".
I don't think I'd want to go as far as 5-word function names, although I expect I have in the past.