Get current Highlight in VS Code Extension - visual-studio-code

I am not talking about the current selection, which can be accessed by vscode.window.activeTextEditor.selection.
When the cursor is inside an identifier, variable name, etc, it becomes highlighted, as shown in this screenshot:
What is this highlight object called? How do I access it?

Searching for everything from "highlight" to "identifier" and whatever else, the answer was more obvious. TextDocument has a getWordRangeAtPosition method, which takes a position and returns the range of the word.
const editor = vscode.window.activeTextEditor;
let cursorPosition = editor.selection.start;
let wordRange = editor.document.getWordRangeAtPosition(cursorPosition);
let highlight = editor.document.getText(wordRange);
// highlight will now contain the currently highlighted word

The thing you are showing is called a Document Highlight. Other instances (of in this case colorData) will also be highlighted. The answer by #Rene Roth is probably answering what you want. And as #Gama11 wrote, using getWordRangeAtPosition without a second parameter uses the "word pattern" of a language. However a document highlight doesn't have to be a single word.
As far as I know you can't get a list of all the highlights? (I've only just figured them out for use in my first extension.)
To highlight like that you need to use registerDocumentHighlightProvider and provideDocumentHighlights. The DocumentHighlightKind can be used to provide different highlight colors (say when an instance of a highlighted variable is on the right or left of an equal sign, i.e. read or written, or for some other reason for distinction). I've used DocumentHighlightKind successfully to show a highlight that's contained within another.

Related

VS Code find-and-replace: is there a way to keep my previous find term when I type ctrl+h?

Currently, when I use ctrl+h with something highlighted, my find term is set equal to the highlighted text. Is there a way to stop that (and keep my find term the same as it was previously)?
Often I want to find-and-replace in VS Code, do something, highlight something, and then find-and-replace the same thing again. Is there a way to make it so that I don't have to retype my find term a second time?
I know there are some plugins that have this functionality; if you know of any that allow me to see both my find and replace terms at the same time, I would like to know.
Set this setting to false:
// Controls if we seed the search string in Find Widget from editor selection
"editor.find.seedSearchStringFromSelection": false,
Editor > Find: Seed Search String From Selection
Doing this will also affect your Find/Search in Files functionality.

How to add a small straight line (I mean like this: a̅ b̅ X̅) onto a character inside a string?

I want to add small straight line onto some desired characters/numbers inside a string inside textview. I couldn't find a solution. Maybe using NSMutableAttributedString. Meanwhile, I mean doing this programmatically. There is strikethrough style, but not overstrike style. Or maybe adding the letters "a" and "_" with different .baseline values. But how to add both characters onto each other then?
Is it possible?
EDIT: Due to make a try for the helpful answers below, I think to make the line at a spesific height is needed. "A\u{0305}" makes the up line very close to the character, as if it sticks. Is there a way to make it at specific height? For example, if we assume that all the keyboard-inputted characters are written inside every single boxes, the ceiling side of these boxes could be lined?
So this (note: see edit below) appears to be an "a tilde ogonek" (it's Lithuanian).
You can write it for instance as follows using these two Unicode characters:
let atildeogonek = "\u{0105}\u{0303}"
let title = "How to add a small straight line (I mean like this: \(atildeogonek)) onto a character inside a string?"
The first character is the a with an ogonek, the second one is the tilde.
EDIT: The initial question specifically asked about the character ą̃ ("a tilde ogonek") in the title, and I used this code to demonstrate how to use Unicode characters in a Swift string. After posting this answer, the question was edited to be more general about "a line above a character".
Programmatically, you could use a function like this:
func overline(character: Character) -> Character? {
return "\(character)\u{0305}".first
}
That will take a character as input and return a new character (glyph) that has had the Unicode combining overline character added to it. It will return nil if adding the combining overline character fails.
The code print(overline(character:"A")!), for example, returns "A̅"
Or, if you want to add an overline to every character in a string, you could use a function like this:
func overline(characters: String) -> [Character?] {
return Array(characters).map { return "\($0)\u{0305}".first
}
}
(I'm not sure if there are any characters for which the above will fail, so I'm not sure if force-unwrapping the result is safe. Thus I left the result of both functions to be optional Character/Array of Character.)
You can easily find the unicodes of ā or ą̃ by using the xcode's own Character Viewer. Just follow the following steps :
hit : Control + Command + SpaceBar
If you get a compact one like this, click the upper right corner icon to expand it.
When expanded, Click the settings gear in the corner . Select customize list.
select Enclosed Characters
Go down to the bottom and open Code tables then add Unicode.
Now, just search for your required Character and you can check its unicode value. here i am searching ā
to print unicode's value :
print("\u{0101}")

VS Code Refactoring: Change all occurences - but only in block scope

When using "change all occurences" in VS Code, it will just search the whole file for matches and change them. Is there a similar feature doing the same thing, but limiting it to function or block scope?
Let's take an example where I would need that: I'm having a React file with several components and want to refactor a class component to a functional component, so I'm changing all occurences of this.props to props. However, I obviously don't want to change all the other class components as well that are supposed to stay class components. :-)
This seems like such a standard use case, but I'm not able to find it anywhere in VS Code. If it's not possible (yet, or for some good reasons) is there another way to achieve what I'm trying to do?
Check out the 'Add Selection To Next Find Match' functionality. It allows you to highlight the first occurrence you'd like to change, then using a keyboard shortcut, highlight the next occurrence and so on until you've selected all the instances you want to change. When all to-be-changed occurrences are selected, you can edit the selected text normally. Just remember to hit the escape key a couple times after editing to return to a single cursor!
Here are the keybindings for the command, it's Cmd+d on Mac:
https://code.visualstudio.com/docs/getstarted/keybindings
I find it very useful when renaming variables, there's also a shortcut to skip occurrences (Cmd+k Cmd+d) in case there is text you don't want to change in between.

Eclipse-RCP: Hide lines of text permanently, keep correct line numbers

I have text files which contain code inside an Editor. The user can run an analysis on a certain part of his code, which will result in a set of lines which should be hidden. Next I want to present the user with only the remaining lines, but with correct linenumbers, as from the original document. Possible solutions I thought of:
Open a new Editor which does not contain the hidden lines, but *somehow* still has correct line numbers
Hide the lines in the original editor, and offer a button for the user to 'unhide'. Probably a similar solution required as in 1.
I don't really know how to go about this. Folds would be a weird solution, because they can be unfolded individually, and seem to be more semantically tied to things like methods or classes. Also, simply creating a new document without the hidden lines results in wrong linenumbers.
Use a ProjectionViewer and reflection to invoke the private method ProjectionViewer.collapse(int offset int length). This method is only used internally to hide a certain portion of the text, by manipulating the ProjectionDocument (see http://eclipse.org/articles/Article-Folding-in-Eclipse-Text-Editors/folding.html).
After this, folding text in the editor using the annotations(the little +/- icons) WILL break everything, so this solution and regular folding are mutually exclusive.

ignore differences in syntax in beyondcompare

In a branch of code I have changed all of the code from obj.varname to obj("varname") and when I compare the code I would like to ignore these differences since varname is the same.
I have a regular expression that I think I need but unfortunately can't get the comparison to be ignored using Beyond Compare from Scooter
^obj\("\w*"\)|obj\.\w*$
I am following this tutorial http://www.scootersoftware.com/support.php?zz=kb_unimportantv3
So my question: is this even possible with beyond compare? If yes, please share a solution including either instructions or post your screenshots.
Beyond Compare 3's Professional edition supports this through its Text Replacements feature. If you've already purchased a Standard edition license you need to revert to trial mode to test it: http://www.scootersoftware.com/suppo...?zz=kb_evalpro
Load your two files in the Text Compare.
Open the Session Settings dialog from the the Session menu, and on the Replacements tab click New to create a new replacement.
In the Text to find edit, use (\w+)\.(\w+)
In the Replace with edit, use $1("$2")
Check the Regular expression checkbox.
The alternative would be to mark any instance of obj.varname and obj("varname") as unimportant. The basic steps would be this:
Load your two files in the Text Compare.
Open the Session Settings dialog from the Session menu, and on the Importance tab click the Edit Grammar... button.
In the next dialog click the New... button below the top listbox.
Change the Element name field to something useful (say, "PropertyAccess").
Change the Category* to List.
In the Text in list* edit, add these two lines:
obj.varname
obj("varname")
Click OK to close the Grammar Item dialog and then click OK again to close the Text Format* grammar item.
Uncheck "PropertyAccess" (or whatever you named it) in the Grammar elements listbox in the Session Settings dialog, then click OK to close it.
This approach isn't as flexible or clean. In the steps above you're matching specific, hardcoded object and variable names, so obj.varname is unimportant but obj.othervar isn't, even if it's aligned against obj("othervar"). If text on both sides is unimportant the difference will be unimportant; if one side is important it will be an important difference. So, with the above steps, obj.varname and obj("varname") will be unimportant everywhere, but it will work correctly since they'll either be matched to other cases that also match those definitions (and thus unimportant) or will be matched to something else that doesn't match that definition, which will be important and will make the difference important.
You can use regular expressions to match more general text categories, but you probably don't want to. For example, if you wanted to match all text that followed that pattern you could use these two lines instead:
\w+\.\w+
\w+\("\w+"\)
And then check the Regular expressions checkbox in the Grammar Item dialog so they're matched that way.
The upside/downside to that is that any text that matches those patterns is then unimportant. abc.newvar vs. def.varname would be considered an unimportant difference because both sides match the unimportant definition. That's good for things like comments or whitespace changes, but probably isn't what you want to do here.