Sublime provides ability to:
select/edit all occurrences of a variable (Quick Find All; alt+F3 on Windows)
select each occurrence one-by-one and then edit the summed total (Quick Add Next; ctrl+d on Windows)
What I want:
select/edit all occurrences within a function's scope
note: I've read this related link (Sublime Text: Select all instances of a variable and edit variable name) and didn't see an answer to how editing might be restricted to function scope.
You can create a fairly simple plugin to do this, making use of Quick Find All, and then just removing any selections that are not inside the current function.
From the Tools menu -> Developer -> New Plugin...
Replace the contents of the new tab with the following:
import sublime
import sublime_plugin
class SelectWordInFunctionCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
prev_sel = [sel for sel in view.sel()]
function_region = next((region for region in view.find_by_selector('meta.function') if region.contains(view.sel()[0])), None)
if function_region:
#word = view.expand_by_class(view.sel()[0].begin(), sublime.CLASS_WORD_START + sublime.CLASS_WORD_END)
view.window().run_command('find_all_under')
sel = [sel for sel in view.sel() if function_region.contains(sel)]
view.sel().clear()
view.sel().add_all(sel if any(sel) else prev_sel)
else:
view.window().status_message('Not inside a function')
instead of using the find_all_under command, use select_word_in_function - you can create a keybinding to do this only when inside a function definition:
{ "keys": ["alt+f3"], "command": "select_word_in_function", "context":
[
{ "key": "selector", "operator": "equal", "operand": "meta.function", "match_all": true },
]
},
Disclaimer: this definitely works with PHP in ST build 3142 and other syntaxes that scope the whole function, but a different approach to detect where the function starts and ends may need to be used for other syntaxes that can't/don't behave this way.
Related
I have a UI that I am creating that replicated Android Studio's (IntelliJ) design system for text as code. What I am trying to achieve is a text that changes it's color multiple times when certain words or patterns happen, such as Kotlin keywords (orange text), extension methods(yellowish text) and variables (purple text).
What do I mean? the following functions text design is what I want to achieve, and they are examples of using all 3 colors -
So I tried to implement it using a buildAnnotatedString { } block and things got A) very messy and B) didn't even reach my goal. Here is my current code of trying to implement keywords and extensions -
// A dummy function that represents only the text part of my entire screen, everything else is irrelevant for the question
#Composable
fun TextAsIntellij(solution : String) {
Text(text = buildAnnotatedString {
val kotlinKeywords = mutableListOf("fun", "val", "var", "if", "return", "null", ",", "class", "while", "break", "continue")
val kotlinExtensions = mutableListOf("forEachIndexed") // will add more as needed
solution.split(" ", ".").forEach { string ->
if (kotlinKeywords.contains(string)) {
withStyle(
style = SpanStyle(
color = Orange
)
) {
append(string)
append(' ')
}
return#forEach
} else if (kotlinExtensions.contains(string)) {
withStyle(
style = SpanStyle(
color = Yellow
)
) {
append(string)
append(" ")
}
return#forEach
}
append(string)
append(' ')
}
})
}
The results in the UI are the following -
As you can see, in the first question I have a problem that before an extension I put a redundant space because I used split() method to identify all words by spaces and dots. For the 2nd picture, I don't yet have a proper solution for identifying varibles from an "object", because I have already split my String using spaces and dots, so diving deeper into another split would make my code really inefficient and I though better can be done. 😁
Hopefully someone has a good idea...any Jetbrains spy in the crowd? ;)
How to add string pieces to existing strings in Sublime Text 3?
Example: I have a text with strings like (23aa67) or (ret457) and I would like to transform them into \as{(23aa67)} or \as{(ret457)}.
Is it possible?
This solution will work if you have at least build number 4107 - you can tell by selecting Help → About Sublime Text.
Open your key bindings by selecting Preferences → Key Bindings. The pane on the right is your user key bindings file, and may simply look like this:
[
]
Position your cursor between the brackets and paste in the following:
{
"keys": ["ctrl+alt+super+a"],
"command": "chain",
"args":
{
"commands":
[
{
"command": "expand_selection",
"args": {"to": "smart"}
},
{
"command": "insert_snippet",
"args": {"contents": "\\as{${0:$SELECTION}}"},
}
]
},
"context":
[
{
"key": "selector",
"operator": "equal",
"operand": "text.tex",
"match_all": true
}
]
}
Here's how it works: The key combo CtrlAltSuperA (where Super is the Windows key) initiates two commands - expand_selection and insert_snippet.
You place your cursor between the two parentheses of your original text, and expand_selection expands the selection to include all of the text plus the opening and closing parens.
The second command wraps the selection - ${0:$SELECTION} - with \as{ at the beginning and } at the end. The "context" at the end only allows the command to run in TeX/LaTeX files. This can be removed if you'd like access to it everywhere.
If you want to change the key binding from CtrlAltSuperA to something else, just be sure that you're not overriding another keybinding. The FindKeyConflicts plugin is great for figuring that out.
Make sure you save the key bindings file when you're done. This shortcut will even work with multiple selections, so you can put multiple cursors in multiple places throughout your text, hit the key combo once, and they'll all be wrapped.
Using regular expression, you can do:
Ctrl+H
Find: (\(.+?\))
Replace: \\as{$1}
Replace all
Explanation:
( # start group 1
\( # opening parens, have to be escaped as it has special meaning in regex
.+? # 1 or more any character, not greedy
\) # closing parens
) # end group 1
Replacement:
\\ # backslash, have to be escaped
as{ # literally
$1 # content of group 1
} # literally
Screenshot (before):
Screenshot (after):
Note: I am not trying to select all instances of a single word.
I can not find a way to select all instances of a word type, similarly how different words are colored according to the format rules from the file type you have.
Here is an image of some sample code of a SQL file.
For example, I would like to Highlight all instances of this reddish pink color, or the baby blue colored words in the whole file. So that way I can capitalize them or copy them or what have you.
From the Tools menu -> Developer -> New Plugin...
Replace the template with the following:
import sublime
import sublime_plugin
class SelectByScopeSelectorCommand(sublime_plugin.TextCommand):
def run(self, edit, scope_selector=None, last_scope_only=True, auto_select=False):
if not scope_selector:
scope_at_first_caret = self.view.scope_name(self.view.sel()[0].a)
if last_scope_only:
scope_at_first_caret = scope_at_first_caret.split()[-1]
if auto_select:
scope_selector = scope_at_first_caret
else:
self.view.window().show_input_panel('Scope Selector', scope_at_first_caret, lambda value: self.view.run_command('select_by_scope_selector', { 'scope_selector': value }), None, None)
return
regions = self.view.find_by_selector(scope_selector)
if regions:
self.view.sel().clear()
self.view.sel().add_all(regions)
self.view.show_at_center(self.view.sel()[0])
else:
self.view.window().status_message('Unable to find anything matching selector "' + scope_selector + '"')
Save it, in the folder ST recommends, as something like select_by_selector.py (the filename doesn't matter too much, but the extension is important).
Then, in your User keybindings, you can add something like:
{ "keys": ["alt+;"], "command": "select_by_scope_selector", "args": { "last_scope_only": true, "auto_select": true } },
Then, pressing Alt+; with the selection caret somewhere in SELECT, it will automatically select all other words in the buffer with the same scope, like DELETE, UPDATE, INSERT etc. Or on INT, it could select all INT, CHAR etc.
You may notice that the "types" you referred to in your question are called scopes in ST parlance. Note that colors don't necessarily have a one to one mapping with scopes depending on your color scheme, so it may select more or less than you expect it to.
You can play around with the keybinding, by removing all arguments for example, to see what effect it has and customize which scopes are being searched for. You can also add it to a menu or the command palette, if that is more to your liking. I suggest to read the ST docs for more info.
I want to know declaration of open bracket when focused close bracket.(ex. if (...) ).
I know emacs, vscode, vim are has goto declaration function. But, they needs 1 action(type M-.(emacs),F12(vscode),%(vim)). I don't want to type some key each time. So, I want to know declaration of bracket with 0-action.
I don't care how displays in declaration(pop-up, mini buffer, status bar)
Background:
I'm in fixing legacy code. The code is too much nested with ifs and fors and whiles.
By much nested, end of code are many continus close bracket(}) like below.
for (var item in list){
if (cond1) {
...
while( cond2 ) {
...
if (cond3) {
...
} else {
...
}
}
}
list.append(item)
}
}
I usually mistake cond2 and cond3, created bugs, don't show log messages, and spent much time.
This question was translated by google translator. so, if you couldn't recognise this, please comment.
When your cursor is on a bracket, the other one is highlighted automatically if you have :help matchparen enabled.
When your cursor is on a bracket, you can jump to the opening one with :help %.
To quote Mass:
Yes- the plugin match-up has this feature:
https://github.com/andymass/vim-matchup
Using the option
let g:matchup_matchparen_offscreen = { 'method': 'popup' }
There is also the ability to show the match in the statusline (the
default):
let g:matchup_matchparen_offscreen = { 'method': 'status' }`
It's really easy to insert a closing brace after typing the opening one:
inoremap { {<CR>}<Esc>ko
This way
if (true) {
converts to
if (true) {
|
}
But I'd like to save time and to type 1 character less:
if (true)<CR>
So I'd like to create the following rule: if return is pressed and the line starts with if/for/while, execute {<CR>}<Esc>ko
Is this doable?
Thanks
Building on your previous mapping, this should do what you want:
inoremap )<CR> ) {<CR>}<Esc>ko
However, you should try a snippet expansion plugin like SnipMate or Ultisnips. Both plugins allow you to define snippets with placeholders and miroring (lots of them are included by default) that are expanded when a <Tab> is pressed after a trigger.
For example, you have a snippet associated with the trigger if that expands into:
if ([condition]) {
}
condition is selected, ready for you to type, and, once you are done, you can hit <Tab> again to jump the cursor between the curly braces, ready to type:
if (myVar == 5) {
|
}
This is incredibly handy.