Smart indent effect on completion (intelliSense) sessions - visual-studio

I am writing a Visual Studio extension which provides intelliSense for a certain content type.
The problem that I am facing now is the effect of "Auto Indent" that Visual Studio provides on empty lines when user types a character.
Here a completion session started on an empty line (over virtual spaces):
Notice the tab symbols on the other lines and no tab on the line with caret on it.
Now when use starts typing, VS automatically and correctly adds necessary tab characters to the line:
Now the problem is those Added tabs apparently become part of the user input and as a result CurrentSession.SelectedCompletionSet.SelectBestMatch() or Filter() method cannot find the current item which starts with "C" here (thinking user has typed \t\tC instead).
If I start the session on anywhere else which does not require auto indent everything works fine.
Any idea?
Edit (more information): I used a code flow very similar to:
Ook here
vsLua here
vsClojure here
In Lua and Clojure you wouldn't face this problem because they never provide intelliSense on virtual spaces (meaning they always start after a certain set of characters) and if you start after a character virtual spaces are already turned into real spaces.
Ook on the other had has the same problem.

Revised Answer:
Ah, I see. I interpreted your question thinking that you were referring to completion triggering via typing, not from the explicit command. If you enable "show whitespace" for the C# editor, you can see what we do here: when you trigger the "show completion" command, we explicitly realize the whitespace so you're no longer floating around in virtual space. You should probably do this as well. (Alternatively, you could detect the scenario and fix it up on the first typing by adjusting your ApplicableTo span, but that's probably not worth the trouble.)
You can get the whitespace that should be inserted from IEditorOperations. So MEF import an IEditorOperationsFactoryService, and then do:
var editorOperations = editorOperationsFactoryService.GetEditorOperations(textView);
var whitespace = editorOperations.GetWhitespaceForVirtualSpace(textView.Caret.Position.VirtualBufferPosition);
if (whitespace.Length != 0)
{
textView.TextBuffer.Insert(textView.Caret.Position.BufferPosition, whitespace);
}
(Funny aside: as I answered this, I was curious to see how we handled this in the Roslyn C# and VB editors. The answer was "not", but filtering still worked by pure luck later in the code.)
Original Answer:
I suspect by your description of the problem that you are implementing your completion like this: you know a character is about to be typed (either via a keyboard filter or IOleCommandTarget) and you trigger an ICompletionSession, where the tracking span is an empty span on the current caret position.
The best approach to fixing this is to not trigger the session before the key is pressed and goes into the editor, but rather after it. This is what we do in the Roslyn implementation for C# and VB completion. Then, when you are in your AugmentCompletionSession call and creating your CompletionSet, compute the "applicable to" span which consists of the non-whitespace characters around your caret. The easiest way to compute this might just be to call GetWordExtent from the text structure navigator.
This allows for other scenarios to work right. Consider scenarios where the user types C, presses escape, and then continues to type your identifier. If you want to trigger completion again, you'd have to do the math to ensure that the "C" is counted as part of your span anyways.

Related

Is there a way to properly dump the navigation history in Visual Studio?

Very frequently, I will be looking for a specific section of code where something happens, and will reach there by starting at a function at a high level of abstraction and go lower by successively opening the code of called methods. Eventually I will find what I'm looking for, and I would like to save the path that I took to get there - which is pretty much what the call stack would be if I had put a breakpoint in that code and stopped here at runtime, except that I'm just inspecting the code.
I'm aware the little arrow next to the "Back" arrow lets me somewhat get that in the UI and I can then take a screenshot of what I'm shown, but that's not a fantastic solution. The names of the functions are trimmed (leading to cases where it could match several functions), the line number is seldom shown (only if there was no code at that line), and I would much rather have the text format to begin with so I can copy the function names into a search tool rather than type them from the screenshot later.
So I was wondering: is there a way to dump the navigation history in Visual Studio ?
Where I could for ex. ask for the last 50 cursor positions, and get the file, file path, line number & possibly the code at that line in text format or some more intelligent thing, should the IDE support that.
Thanks.
PS: I found this very similar question Is there extension for viewing navigation history in Visual studio? that's >5 years old and didn't have a satisfying answer, so I'm trying my luck again, hoping things have changed since if there was no solution back then.

Cursive navigate forward/back word within a comment line

Alt-left & Alt-right (the emacs key-bindings that work across many apps in Mac OS, including as I type now into chrome), instead of doing forward and back word, go forward and back form (i.e. lisp form).
This is a much higher level navigation, frustrating at first, but actually turns out quite usable because you can just bump one char into a form if you don't want to jump over it, then you can navigate by word within it just fine.
This is fine except within a comment line. There, the usual forward and back word break completely, simply jumping you back to the previous form (comment lines are ignored) or on to the next.
It would be great to be able to make cursive/intellij respect forward/back word within a comment line if possible but I have no idea how to modify this particular behaviour. It seems deeper than just modifying a key binding, if I'm correct.
It turns out these keys in cursive/intellij are bound by default as follows (note Alt-right is by default bound to both of the below):
and
The second was overriding the first, the one wanted here.
So by removing the alt binding to make it look like:
Plus the same for back, it now works as desired :)

How to removing annoying auto selection in VS Code

VS Code automatically selects somethings sometimes not only strings inside quotes but also language keywords sometimes. It usually after automatically closing parentheses or after an IntelliSense auto-completion.
This is very much annoying because it prevents further autocompletion when the text gets selected automatically autocomplete dont work.How can I solve this

Reformatting code in text mate to established code conventions - Visual studio's ctrl K+D equivalent on Text Mate

Can anyone tell me if there's a quick way to format your code in Text Mate, similar to pressing ctrl K+D in Visual studio?
Thanks!
Edit by Damien_The_Unbeliever:
For those not familiar with Ctrl K+D, it doesn't just indent code - it reformats it using the generally established formatting conventions in the editor - it may replace spaces with tabs or vice-versa for the indentation, ensure code is consistently indented, move braces to separate lines, etc.
TextMate reindenting and reformatting varies a little depending on the language you're using.
You can generally use the Text menu, that depending wether you have an active selection or not it will show you different commands under it. For example, if you have selected a section of code, there will be a Indent Selection menu item. If you have no active selection, it will be Indent Line.
To have this working properly, be sure to select the current language, if it isn't assigned yet (like on unsaved documents). If you're working with HTML, it will simply indent the lines depending on what's above it. It will keep line breaks intact.
If you need something to break out tags on new lines and properly format the document, you should use the Tidy command that is found in the Bundles menu, under HTML (or simply by using the shortcut CTRL+SHIFT+H. If you have a selection active at the moment that you use it, it will simply reindent that section. If instead you have no selection, it will properly reformat the whole document, including checking for tag validity and other errors.
The Bundles for other language have similar commands, like XML (still Tidy) and Javascript (that has a Reformat Document command).
As an ending note, I simply suggest to look into the Bundles menu; there are many little gems in it. ;)
Did you look in the menu bar? Under Text you have a couple of Reformat… entries that may fit your needs.
Beside these native features, some bundles — like the JavaScript one — have custom Reformat… commands : click on the little cog button at the bottom and explore your current language's bundle's content.

Indenting with white spaces, tabs, and how many spaces or the tab width [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I know that this is more of a coding style, instead of a one right way of doing things. But, I'm a bit frustrated if I came across different indenting formats.
But, I would like to hear the reasons by various people on these issues:
Do you use spaces or tabs? Tabs with spaces? Any difference with "Tab insert space", instead of using the space key directly?
How many spaces to indent each line of code? Why?
Does different code has different style that is more suitable for each of them?
Is there a way to "visually" indent code without actually writing the indent? So it won't ruin the original indenting? It seems unlikely.
I'm using Xcode, so it's better if you have advice for Xcode projects.
You should always use the coding style that the project is already using if you are modifying an existing project.
That being said, if you are able to choose your own coding style for a new project I suggest that you use tabs to indent code and not spaces -- here is why. By using spaces you force all of the other developers working on the project to conform to your indentation preference whether that be 2 spaces or 4 or 8 (or whatever). But by using tabs each developer can view the code using their own preference. You should uses spaces and not tabs to format text (to line up variable names one per line, or to line up multi-line comments) because that will work with any tab width preference. However, when you indent code use tabs not spaces. In short, indent with tabs because indenting with spaces is rude.
spaces because they render the same everywhere
indent-width is context-dependent. runaway indentation is bad. generally, languages where you cannot easily reset the indentation to sane levels through named abstraction items (functions or methods) need narrow indentation.
yes, see previous bullet
the claim that tabs allow individual developers to use different tab widths is false. consider
typedef __LA_SSIZE_T archive_write_callback(struct archive *,
void *_client_data,
const void *_buffer,
size_t _length);
if this was produced with tabwidth of 2, it would look like this with tabwidth = 4:
typedef __LA_SSIZE_T archive_write_callback(struct archive *,
void *_client_data,
const void *_buffer,
size_t _length);
conversely, if it was produced with tabwidth = 8 and displayed with tabwidth = 4, the result would be
typedef __LA_SSIZE_T archive_write_callback(struct archive *,
void *_client_data,
const void *_buffer,
size_t _length);
so if a project style guide requires tabs and says function parameters need to align as in the first code display, then there's a single correct tab width.
(another answer presents the same argument.)
a long-overdue edit:
i wholeheartedly agree with the opening sentence of the accepted answer, which is
You should always use the coding style that the project is already using if you are modifying an existing project.
the rest of that answer is, excuse me, rubbish, and i tried to explain why above.
the question of indentation style should only come up at the very beginning of any project, once it's settled it's done. i do believe that spaces are better than tabs, and have (again, i believe) rational arguments in support of my position, etc. but i'm not going to start discussions on this topic because they're useless: most programmers i met support their preferences with irrational arguments, and the rest have settled for a style which works best with their tooling (like me: "spaces because they render the same everywhere"), which i have no influence over in general.
anecdote: i once worked on a project which underwent a change of newlines, from \r\n to \n, and it turned out to be a pain in the ass any time we needed to go past the revision in svn blame. s/newlines/indentation/, and you have a nice argument against massive indentation changes in a running project.
An advantage to spaces vs tabs is alignment when wrapping long lines. If you use tabs, no matter what you do, the lines below will most likely not align, unless the editor has the same settings.
For example:
result = variable_one + variable_two + variable_three +
variable_four;
If you use tabs, how to ensure that 'variable_four' will show up aligned if tab indentation changes?
There are different programming languages with very different syntax. The syntax is crucial in choosing a coding style. Especially the tab size (number of spaces) that you will chose will depend on how many levels you need to have. In HTML for instance there are many levels, and there it makes no sense to have a big tab size, it even makes no sense in using tabs.. you just use spaces. Another very important aspect is the IDE you use. In some IDE's you are encouraged to use tabs (in Visual Studio you have the nice facility to add/remove tabs to more than one line (TAB to add, SHIFT+TAB to remove) and so on.
Projects have different indent sizes because people use different indent settings (even in the same code) or because they use different editors or because the project includes code from other projects or even simply because people have different preferences or even different sized monitors.
I work in Visual Studio C++ and VB.NET, indent is tab based(3 lines) and tabs do not insert spaces.
I simply use the defaults of the editor I'm currently working in, but if I have the choice I use tabs because it means less invisible characters to manage. I have one editor that wholly manages indentation for me (REALbasic), another editor that manages indentation for me, allows for spaces as prefix but formatting gets wonky if I leave them in (Applescript), and there's Xcode.
I see 4 spaces more than another number for space-based indentation, so I go with that if I have to.
I've found that every community more or less has a standard that has been placed in example code, so I just go with that otherwise tabs then 4 spaces. Simple.
Honestly, I gave up on the indentation fight a long time ago and expend my brain power on other, what I feel are more important, code-related issues like good variable and functions names. Any reasonable code editor can help you manage poorly-placed or complex indentation but none of them can help with sorting out poorly-written or complex code.
Depending on your IDE and your "tab inserts spaces" option, it's a nice shortcut over using the spacebar. I.e. at 3 indentation levels with 3 spaces per tab, you press the Tab key 3 times instead of the spacebar 9 times. The net effect is the same - you inserted 9 spaces - but with 1/3 the number of keystrokes.
A benefit of using spaces instead of tabs can make printing your code a bit nicer, especially if you're using a non-typewriter style font. I usually use a width of 3 spaces for each 'level' of indentation.
Because I prefer proportional fonts at a specific 15pt size, I set a TAB to be 37 pixels.
I don't mind that you use FOUR SPACES as long as you also use a font where a SPACE is 37/4 pixels wide. Or TWO SPACES as long as you also use a font where a SPACE is 37/2 pixels. Or even THREE SPACES, as long as you also use a font where a SPACE is 37/3 pixels.
Otherwise, a TAB works great for both us.

Resources