How to prevent "."(dot) from cancelling autocomplete in Sublime Text 2? - autocomplete

I have defined some keywords for a proprietary language I use at work:
{ "match": "\\b(util.tickettimelimit|util.user_ip|util.server_name|util.today)\\b",
"name": "keyword.source.GTX2",
"comment": "Tags"
}
I also have a completion file:
{
"scope": "source.GTX2",
"completions":
[
"util.server_name",
"util.tickettimelimit",
"util.today",
"util.user_ip"
]
}
When I start typing "util" I see the correct autocomplete options:
But as soon as I enter the "."(dot) autocomplete options go away:
Is there a way to change this behavior? I just want the keywords to be trated as a whole thing and ignore the dots.
Thanks!

I've looked everywhere I can, and it seems the auto-complete code is embedded within the executable itself (at least on Windows, I haven't checked my Mac yet), and not in one of the numerous external .py files scattered around, so I can't even see the parameters for how auto-completion is performed. I looked through the default Packages/Default/Preferences.sublime-settings file and while there are several options relating to auto-complete, there are none relating to what we're looking for. While looking through the Default (Windows).sublime-keymap file in the same directory, I tried adding the following:
{ "keys": ["."], "command": "hide_auto_complete", "context":
[
{ "key": "auto_complete_visible", "operator": "equal", "operand": false }
]
},
but alas it didn't work. There are a number of auto_complete commands there, and while this looked the most promising I haven't tried the rest.
I haven't exhaustively looked through the source and config files for the nifty SublimeCodeIntel plugin (also available through Package Control), so it's possible you might be able to find an option there. You'd probably have to completely disable the built-in auto-complete functionality first, so it doesn't override SCI.
So, I guess for now there's not much you can do. You can always leave a feature request and see if it makes it into Sublime Text 3, or search/open a thread on the Sublime Text Forum and see if anyone else has any suggestions. Good luck!

What I think #Ashish is alluding to is the word_separators setting. You will want to create a syntax specific preference (Preferences -> Settings - More -> Syntax Specific - User). Create a word_separators entry with the dot removed (Copy from the default preferences as the base). This will give you the behavior you want but there are some things to note. The dot, obviously, will not be treated as a word separator, which will change some behavior.
I'll use java as an example. If I had a variable foo, with some method bar, I could enter foo.b and bar would be shown as a completion. Without the dot as a separator, you will not see this.
Another example, perhaps easier to understand is when selecting words. If you use ctrl/cmd + d to select the word, it selects words, bound by word separators. So if I had foo.ba|r, where the | represents the cursor position and used ctrl/cmd+d it would select bar. With the dot removed as a word separator, foo.bar would be selected.
Let me know if I can clarify anything.

It's a little late but I hope this can help, create a new plugin and add this code:
import re
myObjects = {"util": ["server", "tickettimelimit", "today", "user_ip"]}
class CustomAutocomplete(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
if not view.match_selector(0, "source.GTX2"):
return
if prefix == "":
# get the last word in the current line
currentposition = view.sel()[0].begin()
currentline = view.full_line(currentposition)
text = view.substr(currentline)
words = re.findall("\w+", text)
lastword = words[-1]
if lastword in myObjects.keys():
# return a list of tuples, it represents snippets
# the first element is the trigger and the second one
# is the content
return [(x, x) for x in myObjects[lastword]]
return []
And add the next key in the user settings:
"auto_complete_triggers":
[
{
"characters": ".",
"selector": "source.GTX2"
}
]

Don't press . (dot) else you will need to type at least one character after dot so list can appear again. Using Brackets or Dot tells Sublime Text 2 that user has completed typing.
example: if I type for then sublime will show dropdown list but if I type for( list will disappear.

Click on Preferences > Settings - User, then copy and paste the following
// Characters that are considered to separate words – does not include periods.
// Place comma at the end of the line if there are multiple keybindings.
"word_separators": "/\\()\"‘-:,;~!##$%^&*|+=[]{}`~?"
From this webpage:
http://tomschenkjr.net/using-sublime-text-2-for-r/

Related

VSCode Intellisense: can we add keywords to an "exclude" list that won't be auto-completed?

I'm writing C code in VSCode with the amazing Tab Out extension that let you tab out of brackets, quotes etc. Together with the automatically added end brackets that VSCode has,I feel very comfortable writing C code when I can just do tab, tab, semi-conlon to end a line. However, every time I try to write self at the end near the ), I can't tab out without it being changed to STDERR_FILENO due to autocompletion.
How can I add a word, such as self, so that Intellisense will not autocomplete it? Is there a list of "excluded" words that I can tell Intellisense to not mess with?
I don't want to turn off any of the autocomplete stuff, I just want some very specific keywords to not be replaced when I tab out of stuff. I also don't want to stop using tab to autocomplete. these features are all very handy.
My current work-around is defining a new code snippet in the C snippet file:
"self": {
"body": "self",
"prefix": "self",
"description": "this is to prevent stupid suggestions for the keyword self"
}
but I consider this an ugly fix so let me know if you can help!

How to hide options menu when cursor is inside single or double quotes in sublimetext

I am trying to use the autofilename plugin, but for some reason it replaces me or incorrectly corrects the file path, I think I know that it is because of the options that automatically appear as references when the cursor is inside single or double quotes by example:
As you can see, several options that do not exist such as directory or file names are shown, those names are mostly names that exist in class and id, among other things.
So how can it be avoided that only when the cursor is inside single or double quotes that these options do not appear.
As I mentioned I am trying to use the autofilename plugin but it automatically deletes or corrects me wrong data, I am using sublime text 4113
Probably the easiest way to do this is by assigning a custom key mapping to the filename autocomplete function. First, select Preferences → Key Bindings and add the following, ensuring that they are inside the outer [ ] characters (the file has to be valid JSON):
{
"keys": ["ctrl+super+alt+a"],
"command": "afn_show_filenames",
"context":
[
{
"key": "afn_use_keybinding",
"operator": "equal",
"operand": true
}
]
},
You can change the key combo to whatever you wish, just make sure it's not overwriting another Sublime or operating system key binding.
Next, open Preferences → Package Settings → AutoFileName → Settings—User and add the following line:
"afn_use_keybinding": true,
Now, to open the filename autocomplete, simply press CtrlWinAltA (on Windows and Linux) or Ctrl⌘AltA (on OSX/macOS).

Is there a keyboard shortcut to select word occurrences in just one line in visual studio code

I know the topic of multi cursors-editing in visual studio code is duplicate but what I want is the way (by keyboard) to select word occurrences in just one line in visual studio code, because the other option: ctrl + F2 it selects all the occurrences in all the file, and not the mouse way by holding alt and click.
let say I have this:
const FETCH_USERS_REQUEST = "FETCH_USERS_REQUEST";
const FETCH_USERS_REQUEST = "FETCH_USERS_REQUEST";
when the cursor is on REQUEST word for the second line I want to do two cursors in the second line after the two occurrences of REQUEST.
You have 2 alternatives:
Enable the Find in Selection option in the Find Widget after entering your find query.
An extension that I wrote does this fairly well, see Find and Transform.
With this simple keybinding:
{
"key": "alt+y", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"restrictFind": "line" // find all on current line only
// with multiple cursors you can have as many current lines as you wish
}
}
It does a find in the current file. Since there is no actual find query, like REQUEST, designated in the args. It will find the current word at the cursor on the line. Different languages define what a "word" is differently. For javascript, for example, FETCH_USERS_REQUEST is the current word even if the cursor is on Request only.
You can manage this by actually selecting, double-clicking on Request or Ctrl+D, and then trigger the above keybinding. Then the extesnion will search for whatever is selected, if there is a non-empty selection.
The extension is designed to select those find matches, not put the cursor after them, but you can simply right-arrow to dismiss the selection and the cursor will be where you want.
[I have to update the extension, the current version v0.9.7 in the Marketplace won't do this - but here is a demo of it already working. It should be updated tomorrow at the latest, look for v0.9.8.]

Is there a way to select code between certain characters

TL DR
Is there some extension, or other functionality, to select code above and below the cursor that is surrounded by some characters, e.g. %% or % ===== or anything else?
Why
I am coding Matlab in VSCode, since the builtin Matlab editor lacks a lot of useful functionalities. However, One thing that I miss is the ability to run 'sections':
%% section 1
x = 0:0.01:pi;
y = sin(y);
plot(x,y);
%% section 2
x = 0:0.01:pi;
y = cos(y);
plot(x,y);
%% section 3
x = 0:0.01:pi;
y = tan(y);
plot(x,y);
A section can be run using Ctrl+Enter. Since I use Matlab on Windows, there is no support for running the Matlab interpreter from the terminal/cmd.
So I created some AutoHotKey scripts that will copy selected code, open the Matlab window, paste it, and run. This works fine, however, when I want to run a very large section, this means that I have to select this manually, and press my defined shortcut to call the AutoHotkey script. Hence above question.
So say I have the cursor at some line insection 2, how can I select only the code in this section (including or excluding section 'headers', these are comments so does not matter).
I have made an extension Select By that allows the selection based on regular expressions relative to the cursor position. You can specify different expressions for forward and backward search and if you want to include the searched text in the selection.
You can set up to 5 regex searches.
The extension defines 5 commands that can be used in keyboard shortcuts
This can (of course) be done by writing a custom extension.
Since this seems like useful functionality in general and an interesting exercise, I've added the required capability to my personal vscode-smcpeak extension.
To use it:
Install vscode-smcpeak. This has to be installed manually for now. At time of writing, 0.0.7 is latest.
Install macros by ctf0. This one can be installed from within VSCode. Restart VSCode after installing it. (See below regarding the choice of "macros" extension.)
Add to your settings.json (File → Preferences → Settings, then click the "Open Settings (JSON)" icon in the upper right):
// Custom macros for ctf0.macros extension
"macros.list": {
// https://stackoverflow.com/questions/57749780/is-there-a-way-to-select-code-between-certain-characters
"selectPercentRegion": [
{
"command": "smcpeak.goToLineMatching",
"args": {
"regex": "^%%",
"moveUp": true,
"select": false,
"allowZeroMove": true
}
},
{
"command": "smcpeak.goToLineMatching",
"args": {
"regex": "^%%",
"moveUp": false,
"select": true,
"allowZeroMove": false
}
},
"smcpeak.revealCurrentSelection"
]
},
Add to keybindings.json (File → Preferences → Keyboard Shortcuts, then click the "Open Keyboard Shortcuts (JSON)" icon in the upper right):
{
"key": "ctrl+alt+m", // or whatever
"command": "macros.selectPercentRegion"
},
(Note: VSCode will always complain that macros.selectPercentRegion is not a valid command. That's a limitation of the "macros" extension.)
Then, with the cursor within a region delimited by the specified regexes, press the keybinding to activate the macro:
Why use two extensions?
The accepted answer, by rioV8 uses a single extension to answer the OP's question. This looks like a good solution to me, and is simpler to use than mine, so I approve of that being the accepted answer.
However, there is an advantage to my approach: it allows the behavior to be modified in settings.json. For example, the OP asked in a follow-up question about having the command additionally copy the text to the clipboard after selecting. Using the macro-based solution, this is an easy modification to settings.json; simply add:
"editor.action.clipboardCopyAction"
to the end of the selectPercentRegion macro. (That is the command normally bound to Ctrl+C.)
Thus, while my suggestion has more moving parts, I personally prefer this approach.
Which macros extension to use?
As Mark noted in a comment, the most popular macros extension, geddski.macros, is unmaintained. It furthermore has a rather serious bug in that the commands are not properly sequenced, leading to race conditions. (I actually saw this once during my testing.)
There are two forks of geddski.macros on the marketplace, l7ssha.macrosRe and ctf0.macros. l7ssha.macrosRe only adds the "delay" ability, which is an awkward way to resolve the race conditions. ctf0.macros, on the other hand, fully fixes the race condition (via a complete rewrite).
Therefore, I recommend ctf0.macros, and in fact have edited my answer to use it rather than the original geddski.macros extension.
However, be aware:
ctf0.macros uses macros.list rather than macros in settings.json
ctf0.macros requires a restart of VSCode after installation to activate
How it works
The additions are in vscode-smcpeak commit 6d5f5d4689.
My extension registers a new command, smcpeak.goToLineMatching, that searches up or down for a line containing a regex. The logic is very simple, just a loop and regex test.
It also registers smcpeak.revealCurrentSelection, needed because otherwise the selected text might be offscreen and nothing scrolls to it by default.
The macro selectPercentRegion uses goToLineMatching to look upwards for one regex, setting the anchor (selection start) there, then uses it again to look down for another regex (although they are the same here), extending the selection to that point. Then it scrolls to ensure the selection is visible.
It should be straightforward to transplant this code to your own extension if you don't want the rest of the stuff that mine provides. (But mine does not bind any keys, so there should be no downside to having the extra stuff.)
Here is another way to do this, with an extension I wrote Find and Transform.
Make this keybinding in your keybindings.json (or it could be a setting too):
{
"key": "alt+m", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"find": "^(%%[\\s\\S]+?)(?=^$)|(^%%[\\s\\S]+)",
"run": [
"$${",
"const sections = vscode.window.activeTextEditor.selections;",
"await vscode.commands.executeCommand('cursorUndo');",
"const mySection = sections.find(section => section.contains(vscode.window.activeTextEditor.selection));",
"if (mySection) {",
"vscode.window.activeTextEditor.selections = [mySection];"
"}",
"}$$",
],
"isRegex": true,
"postCommands": "editor.action.clipboardCopyAction"
}
}
The find regex finds and selects all %% .... sections. But since you only want the section where your cursor started, some code is run to restore that original cursor and then find that section that contains the one current cursor. Then that section is selected. And in the postCommand that selection is saved to the clipboard.
That is using javascript and the vscode extension api within a keybinding or setting.

how to add different number at end of multi line edit?

Having trouble finding a way to do this, maybe it is not even possible?
In my case, for testing flow of if-statements/user-interaction, am temporarily adding 40 lines of console.log('trigger-fired-1'); throughout our code.
However, to tell them apart would like each to end with a different number, so in this case, numbers one to forty like so:
In the screen recorded gif, to replicate what I am going for, all I did was copy/paste the numbers one to nine. What I really would like is a shortcut key to generate those numbers at the end for me to eliminate that step of typing out each unique number.
Am primarily coding in Visual Studio Code or Sublime Text, and in some cases shortcuts are similar, or at least have same support but for different shortcut keys.
There are a few extensions that allow you to do this:
Text Pastry
Increment Selection
NumberMonger
For Sublime Text, the solution to this problem is the internal Arithmetic command. Something similar may or may not be available in VS Code (possibly with an extension of some sort) but I'm not familiar enough with it to say for sure.
This command allows you to provide an expression of some sort to apply to all of the cursor locations and/or selected text.
By way of demonstration, here's the example you outlined above:
The expression you provide is evaluated once for every selection/caret in the buffer at the time, and the result of the expression is inserted into the buffer (or in the case of selected text, it replaces the selection). Note also that when you invoke this command from the input panel (as in the screen recording) the panel shows you a preview of what the expression output is going to be.
The special variable i references the selection number; selections are numbered starting at 0, so the expression i + 1 has the effect of inserting the selection numbers starting at 1 instead of 0.
The special variable x refers to the text in a particular selection instead. That allows you to select some text and then transform it based on your expression. An example would be to use x * 2 immediately after the above example (make sure all of the selections are still present and wrapping the numbers) to double everything.
You can use both variables at once if you like, as well as anything in the Python math library, for example math.sqrt(i) if you want some really esoteric logs.
The example above shows the command being selected from the command palette interactively, where the expression automatically defaults to the one that you want for your example (i + 1).
If you want to have this as a key binding, you can bind a key to the arithmetic command and provide the expression directly. For example:
{
"keys": ["super+a"],
"command": "arithmetic",
"args": {
"expr": "i+1"
},
},
Try this one ...
its not like sublime
but works g
https://github.com/kuone314/VSCodeExtensionInsertSequence