Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Cmd-Shift-D behaviour #105

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -239,7 +239,7 @@
"win": "ctrl+shift+d",
"linux": "ctrl+shift+d",
"key": "ctrl+shift+d",
"command": "editor.action.copyLinesDownAction",
"command": "extension.duplicate",
"when": "editorFocus"
},
{
Expand Down
33 changes: 33 additions & 0 deletions src/actions.ts
@@ -0,0 +1,33 @@
import * as vscode from "vscode";

export function duplicateAction(editor: vscode.TextEditor) {
if (!hasActiveSelection(editor)) {
vscode.commands.executeCommand("editor.action.copyLinesDownAction");
} else {
duplicateWithSelection(editor);
}
}

function hasActiveSelection(editor: vscode.TextEditor): boolean {
for (var i = 0; i < editor.selections.length; i++) {
if (!editor.selections[i].isEmpty) {
return true;
}
}
return false;
}

function duplicateWithSelection(editor: vscode.TextEditor) {
var sel = editor.selection;
let text = editor.document.getText(sel)

editor.edit(edit => {
edit.insert(sel.active, text);
}).then(() => {
try {
editor.selection = new vscode.Selection(sel.active, editor.selection.active);
} catch (err) {
console.log(err)
}
})
}
4 changes: 4 additions & 0 deletions src/extension.ts
@@ -1,6 +1,7 @@
import * as vscode from 'vscode';
import { readFileAsync } from './fsWrapper';
import { Mapper } from './mapper';
import { duplicateAction } from './actions';
import { ISetting, MappedSetting, CategorizedSettings, VscodeSetting } from './settings';
import * as sublimeFolderFinder from './sublimeFolderFinder';
import * as path from 'path';
Expand All @@ -14,6 +15,9 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
await showPrompt();
await context.globalState.update('alreadyPrompted', true);
}

// register custom commands
context.subscriptions.push(vscode.commands.registerTextEditorCommand("extension.duplicate", duplicateAction));
}

async function showPrompt(): Promise<void> {
Expand Down