How to make emacs paste the clipboard content
call_in_the_middle('arg')
call_something()
call_else()
into the code
function my function(a,b,c) {
hello('world')
| <---------cursor position
bye('world)
}
to get the result immediately as:
function my function(a,b,c) {
hello('world')
call_in_the_middle('arg')
call_something()
call_else()
bye('world)
}
but not
function my function(a,b,c) {
hello('world')
call_in_the_middle('arg')
call_something()
call_else()
bye('world)
}
?
UPD It's not about the editing modes formatting - I want to paste text shifted by by cursor left position in any mode
Related
I'm going to get text input using TextField and the following is my code:
public Text textInput;
void Update()
{
if (Input.GetKeyDown(KeyCode.Return)) {
if (textInput.text == "go") {
/* do something */
textInput.text = "";
}
}
}
I assigned the Text element under TextField to public Text textInput, and I expected that once a player types "go" and presses enter, the TextField will be cleared and the player be able to type next sentence.
However, after I type "go" and press enter, the text in TextField remained and the focus was out.
How can I clear the TextField maintaining the focus on it?
I think the "problem" is that the input field by default also handles the Return key as the submission command which automatically loses the focus.
The default InputField.lineType is
SingleLine
Only allows 1 line to be entered. Has horizontal scrolling and no word wrap. Pressing enter will submit the InputField.
You could instead set it to MultiLineNewLine
Is a multiline InputField with vertical scrolling and overflow. Pressing the return key will insert a new line character.
So it doesn't automatically call the OnSubmit and lose focus when pressing the Return key.
Using that as an alternative to use Update and GetKeyDown you could use InputField.onValidateInput and do something like
public InputField textInput;
private void Awake ()
{
textInput.lineType = InputField.LineType.MultiLineNewLine;
textInput.onValidateInput = OnValidate;
}
private char OnValidate(string text, int charIndex, char addedChar)
{
if(addedChar == '\n')
{
if(text.Equals("go"))
{
// Do something
}
textInput.text = "";
return '';
}
return addedChar;
}
I'm looking for a simple method to determine the column position of a VSCode editor's text insertion cursor/caret, either prior to selecting an area of text to copy or immediately the mouse is used to start the selection. The column number would then be stored in the clipboard before performing further clipboard manipulation.
I have tried searching for AutoHotkey methods to achieve this, but the only solution I'm able to find involves using ImageSearch, which is not suitable for my purpose.
Edit: I found this API reference, could I possibly use this to determine the cursor position preferably using windows cmd/powershell?
You can make a vscode extension and bind the command to a keyboard shortcut.
How to get line and column(character) of cursor:
const activeEditor = vscode.window.activeTextEditor
if (activeEditor) {
console.log(activeEditor.selection.active.line)
console.log(activeEditor.selection.active.character) //column
}
api: https://code.visualstudio.com/api/references/vscode-api#details-159
activeEditor.selection gives you an object with 4 objects
start:Object
line:4
character:8
end:Object
line:6
character:8
active:Object
line:4
character:8
anchor:Object
line:6
character:8
activeEditor.selection.active is your cursor.
activeEditor.selection.anchor is
The position at which the selection starts. This position might be before or after active.
anchor-active may be reversed, but start-end will always be up-down.
note: I found the api AFTER I found out how to get current line from here:
VScode API why can't I get the current line?
EDIT: for columnNum (see Eric's comment):
const activeEditor = vscode.window.activeTextEditor
if (activeEditor) {
const lineOffset = activeEditor.selection.active.line
const charOffset = activeEditor.selection.active.character
console.log(`line: ${lineOffset + 1}`)
console.log(`character: ${charOffset + 1}`)
console.log(`column: ${getColumn(activeEditor.document.lineAt(lineOffset).text,charOffset) + 1}`) //column
function getColumn(str, sumCharacter) {
const arr = [...str]
let whichCharacter = 0
for (let whichColumn = 0; whichColumn < arr.length; whichColumn++) {
if (whichCharacter===sumCharacter) {
return whichColumn
}
whichCharacter+=arr[whichColumn].length
}
return arr.length
}
}
I'm trying to write an extension for VSCode where the editor screen is always centered on the cursor. There are other extensions that add a command to center the screen onto the cursor, but you have to press the command to activate it.
Currently the only way I've found to implement this is to rewrite the cursorUp, cursorDown, enter, pageUp, pageDown -- any command that moves the cursor up and down basically, and then use the "revealLine" command with the cursor line position and with the "at" attribute as "center".
Is there a better way? Reimplementing the default editor commands seems very inefficient.
Here's what I've got currently:
"use strict";
import * as vscode from "vscode";
export function activate(context: vscode.ExtensionContext) {
let disposable1 = vscode.commands.registerCommand("cursorUp",() => {
centralizar();
vscode.commands.executeCommand("cursorMove", {
to: "up",
});
}
);
let disposable2 = vscode.commands.registerCommand("cursorDown",() => {
centralizar();
vscode.commands.executeCommand("cursorMove", {
to: "down",
});
}
);
context.subscriptions.push(disposable1);
context.subscriptions.push(disposable2);
}
function centralizar() {
let currentLineNumber = vscode.window.activeTextEditor.selection.start.line;
vscode.commands.executeCommand("revealLine", {
lineNumber: currentLineNumber,
at: "center"
});
}
export function deactivate() {}
Perhaps you can utilize the new (v1.38) command editor.cursorSurroundingLines. See scrollOff release notes.
It takes a number, but if you can get the number of visible lines in an editor window, then set editor.cursorSurroundingLines to the appropriate midpoint that may work. It seems you would not have to listen for scroll events. But would have to update the value if the editor window was resized.
The Scrolloff extension implements this functionality when its "alwaysCenter" option is enabled. It works by listening to window.onDidChangeTextEditorSelection, which fires when the cursor moves.
In a normal buffer of nvim, the following ex command works.
:normal! GAabcd
This adds the text "abcd" to the current normal buffer.
But in a terminal buffer of nvim, the above ex command doesn't work!
:normal! GAabcd
This just converts the terminal buffer into '-- TERMINAL --' mode and the "abcd" isn't added!
Why isn't the text added? How can I accomplish that?
I digged the neovim sources and maybe found the answer by myself.
In edit funtion of edit.c, there is the following comment.
bool edit(int cmdchar, bool startln, long count)
{
if (curbuf->terminal) {
if (ex_normal_busy) {
// Do not enter terminal mode from ex_normal(), which would cause havoc
// (such as terminal-mode recursiveness). Instead set a flag to force-set
// the value of `restart_edit` before `ex_normal` returns.
restart_edit = 'i';
force_restart_edit = true;
} else {
terminal_enter();
}
return false;
}
...
I think it means that it's not possible to enter terminal mode(insert mode) while processing normal commands in ex-mode. Therefore the text 'abcd' wasn't added in terminal mode but was just processed as normal commands.
Your answer is correct in that it's not possible to append text using :normal! GAfoo. However, in the interests of answering your last question, you can do this:
:normal! "0p
So if you happen to have what you need in a register, you can always paste it.
I am working on customizing the codemirror for my new language mode. As part of this new mode implementation, I am writing a new tool bar where user can select some text and say insert. This command should insert the text where user was typing just before clicking on tool bar.
I could not find any API level support to do so. If there is any other way can someone help me out on this?
Basically get the current cursor positio- line number and position at which cursor is currently present. May be a Position object
API for inserting a text, something like insertText("Text", PositionObject)
Here's how I did it:
function insertTextAtCursor(editor, text) {
var doc = editor.getDoc();
var cursor = doc.getCursor();
doc.replaceRange(text, cursor);
}
How about replaceSelection (http://codemirror.net/doc/manual.html#replaceSelection)?
doc.replaceSelection(replacement: string, ?select: string)
Replace the selection(s) with the given string. By default, the new selection ends up after the inserted text. The optional select argument can be used to change this—passing "around" will cause the new text to be selected, passing "start" will collapse the selection to the start of the inserted text.
To add the new line at the end -
function updateCodeMirror(data){
var cm = $('.CodeMirror')[0].CodeMirror;
var doc = cm.getDoc();
var cursor = doc.getCursor(); // gets the line number in the cursor position
var line = doc.getLine(cursor.line); // get the line contents
var pos = { // create a new object to avoid mutation of the original selection
line: cursor.line,
ch: line.length - 1 // set the character position to the end of the line
}
doc.replaceRange('\n'+data+'\n', pos); // adds a new line
}
Call function
updateCodeMirror("This is new line");
Improved function that, if selection present, replaces the text, if not, inserts in current cursor position
function insertString(editor,str){
var selection = editor.getSelection();
if(selection.length>0){
editor.replaceSelection(str);
}
else{
var doc = editor.getDoc();
var cursor = doc.getCursor();
var pos = {
line: cursor.line,
ch: cursor.ch
}
doc.replaceRange(str, pos);
}
}
You want to use the replaceRange function. Even though the name says "replace", it also serves as "insert" depending on the arguments. From the documentation at the time I write this:
Replace the part of the document between from and to with the given
string. from and to must be {line, ch} objects. to can be left off to
simply insert the string at position from. When origin is given, it
will be passed on to "change" events, and its first letter will be
used to determine whether this change can be merged with previous
history events, in the way described for selection origins.
Final function to insert text at current cursor position in a performant way.
Hope it helps.
function insertStringInTemplate(str)
{
var doc = editor_template.getDoc();
var cursor = doc.getCursor();
var pos = {
line: cursor.line,
ch: cursor.ch
}
doc.replaceRange(str, pos);
}
This function is used to insert to the specified position and move the cursor to the end of the inserted text.
function insertToCodeMirror(text) {
const doc = codeMirrorInstance.getDoc();
const cursor = codeMirrorInstance.getCursor();
doc.replaceRange(text, cursor);
codeMirrorInstance.focus();
setTimeout(() => {
cursor.ch += text.length;
codeMirrorInstance.setCursor(cursor);
}, 0);
}