Skip to content

Commit 0ad35f5

Browse files
author
申蛟隆
committed
支持backspace多行删除
1 parent 1c09447 commit 0ad35f5

File tree

5 files changed

+255
-8
lines changed

5 files changed

+255
-8
lines changed

dist/client.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22883,6 +22883,33 @@ var require_framework = __commonJS({
2288322883
var require_assist = __commonJS({
2288422884
"out/assist.js"(exports2) {
2288522885
"use strict";
22886+
var __awaiter = exports2 && exports2.__awaiter || function(thisArg, _arguments, P, generator) {
22887+
function adopt(value) {
22888+
return value instanceof P ? value : new P(function(resolve) {
22889+
resolve(value);
22890+
});
22891+
}
22892+
return new (P || (P = Promise))(function(resolve, reject) {
22893+
function fulfilled(value) {
22894+
try {
22895+
step(generator.next(value));
22896+
} catch (e) {
22897+
reject(e);
22898+
}
22899+
}
22900+
function rejected(value) {
22901+
try {
22902+
step(generator["throw"](value));
22903+
} catch (e) {
22904+
reject(e);
22905+
}
22906+
}
22907+
function step(result) {
22908+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
22909+
}
22910+
step((generator = generator.apply(thisArg, _arguments || [])).next());
22911+
});
22912+
};
2288622913
Object.defineProperty(exports2, "__esModule", { value: true });
2288722914
var vscode_12 = require("vscode");
2288822915
var Assist = class {
@@ -22896,6 +22923,105 @@ var require_assist = __commonJS({
2289622923
this.explorer.context.subscriptions.push(vscode_12.commands.registerCommand("vue-helper.funcEnhance", () => {
2289722924
this.funcEnhance();
2289822925
}));
22926+
this.explorer.context.subscriptions.push(vscode_12.commands.registerCommand("vue-helper.backspace", () => {
22927+
this.backspce();
22928+
}));
22929+
}
22930+
asNormal(key, modifiers) {
22931+
switch (key) {
22932+
case "enter":
22933+
if (modifiers === "ctrl") {
22934+
return vscode_12.commands.executeCommand("editor.action.insertLineAfter");
22935+
} else {
22936+
return vscode_12.commands.executeCommand("type", { source: "keyboard", text: "\n" });
22937+
}
22938+
case "tab":
22939+
if (vscode_12.workspace.getConfiguration("emmet").get("triggerExpansionOnTab")) {
22940+
return vscode_12.commands.executeCommand("editor.emmet.action.expandAbbreviation");
22941+
} else if (modifiers === "shift") {
22942+
return vscode_12.commands.executeCommand("editor.action.outdentLines");
22943+
} else {
22944+
return vscode_12.commands.executeCommand("tab");
22945+
}
22946+
case "backspace":
22947+
return vscode_12.commands.executeCommand("deleteLeft");
22948+
}
22949+
}
22950+
backspce() {
22951+
var _a, _b, _c, _d, _e, _f, _g;
22952+
return __awaiter(this, void 0, void 0, function* () {
22953+
let editor = vscode_12.window.activeTextEditor;
22954+
if (!editor) {
22955+
this.asNormal("backspace");
22956+
return;
22957+
}
22958+
if (((_a = vscode_12.window.activeTextEditor) === null || _a === void 0 ? void 0 : _a.selections.length) && ((_b = vscode_12.window.activeTextEditor) === null || _b === void 0 ? void 0 : _b.selections.length) > 1) {
22959+
let selections = (_c = vscode_12.window.activeTextEditor) === null || _c === void 0 ? void 0 : _c.selections;
22960+
let selectionList = [];
22961+
for (let index = 0; index < selections.length; index++) {
22962+
const selection = selections[index];
22963+
if (selection.start.line === selection.end.line && selection.start.character === selection.end.character) {
22964+
if (selection.anchor.character > 0) {
22965+
selectionList.push(new vscode_12.Selection(new vscode_12.Position(selection.anchor.line, selection.anchor.character - 1), selection.anchor));
22966+
} else if (selection.anchor.line > 0) {
22967+
let len = editor.document.lineAt(selection.anchor.line - 1).text.length;
22968+
selectionList.push(new vscode_12.Selection(new vscode_12.Position(selection.anchor.line - 1, len), selection.anchor));
22969+
}
22970+
} else {
22971+
selectionList.push(selection);
22972+
}
22973+
}
22974+
yield editor.edit((editBuilder) => {
22975+
for (let i = 0; i < selectionList.length; i++) {
22976+
editBuilder.delete(selectionList[i]);
22977+
}
22978+
});
22979+
return;
22980+
}
22981+
if (((_d = vscode_12.window.activeTextEditor) === null || _d === void 0 ? void 0 : _d.selection.start.line) === ((_e = vscode_12.window.activeTextEditor) === null || _e === void 0 ? void 0 : _e.selection.end.line) && ((_f = vscode_12.window.activeTextEditor) === null || _f === void 0 ? void 0 : _f.selection.start.character) === ((_g = vscode_12.window.activeTextEditor) === null || _g === void 0 ? void 0 : _g.selection.end.character)) {
22982+
if (editor.selection.anchor.line === 0) {
22983+
if (editor.selection.anchor.character > 0) {
22984+
yield editor.edit((editBuilder) => {
22985+
editBuilder.delete(new vscode_12.Selection(new vscode_12.Position(editor.selection.anchor.line, editor.selection.anchor.character - 1), editor.selection.anchor));
22986+
});
22987+
}
22988+
} else {
22989+
let isLineEmpty = editor.document.lineAt(editor.selection.anchor.line).text.trim() === "";
22990+
if (isLineEmpty) {
22991+
let preText = "";
22992+
let line = editor.selection.anchor.line;
22993+
while (preText.trim() === "" && line >= 0) {
22994+
line -= 1;
22995+
preText = editor.document.lineAt(line).text;
22996+
}
22997+
yield editor.edit((editBuilder) => {
22998+
editBuilder.delete(new vscode_12.Selection(new vscode_12.Position(line, preText.length), editor.selection.anchor));
22999+
});
23000+
} else {
23001+
let startPosition;
23002+
let endPosition = editor.selection.anchor;
23003+
let preLineText = editor.document.getText(new vscode_12.Range(new vscode_12.Position(endPosition.line, 0), endPosition));
23004+
if (endPosition.character === 0 || preLineText.trim() === "") {
23005+
startPosition = new vscode_12.Position(endPosition.line - 1, editor.document.lineAt(endPosition.line - 1).text.length);
23006+
} else {
23007+
startPosition = new vscode_12.Position(endPosition.line, endPosition.character - 1);
23008+
let txt = editor.document.getText(new vscode_12.Range(new vscode_12.Position(endPosition.line, endPosition.character - 1), endPosition));
23009+
if (editor.document.lineAt(endPosition.line).text.length > endPosition.character) {
23010+
let nextTxt = editor.document.getText(new vscode_12.Range(endPosition, new vscode_12.Position(endPosition.line, endPosition.character + 1)));
23011+
if (txt === "{" && nextTxt === "}" || txt === "(" && nextTxt === ")" || txt === "'" && nextTxt === "'" || txt === '"' && nextTxt === '"' || txt === "[" && nextTxt === "]" || txt === "<" && nextTxt === ">") {
23012+
endPosition = new vscode_12.Position(endPosition.line, endPosition.character + 1);
23013+
}
23014+
}
23015+
}
23016+
yield editor.edit((editBuilder) => {
23017+
editBuilder.delete(new vscode_12.Selection(startPosition, endPosition));
23018+
});
23019+
}
23020+
}
23021+
} else {
23022+
this.asNormal("backspace");
23023+
}
23024+
});
2289923025
}
2290023026
funcEnhance() {
2290123027
var _a, _b, _c, _d;

package.json

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@
4545
],
4646
"contributes": {
4747
"commands": [
48-
{
49-
"command": "vue-helper.searchFile",
50-
"title": "%extentions.searchFile%"
51-
},
5248
{
5349
"command": "vue-helper.backspace",
5450
"title": "vue-helper: backspace"
@@ -72,8 +68,23 @@
7268
"key": "shift+alt+enter",
7369
"command": "vue-helper.funcEnhance",
7470
"when": "editorTextFocus"
71+
},
72+
{
73+
"key": "backspace",
74+
"command": "vue-helper.backspace",
75+
"when": "textInputFocus && editorLangId =~ /^vue$|^typescript$|^javascript$|^html$|^json$|^css$|^scss$/"
76+
}
77+
],
78+
"configuration": {
79+
"title": "vue-helper",
80+
"type": "object",
81+
"properties": {
82+
"vue-helper.use-vue-snippets": {
83+
"type": "boolean",
84+
"default": true
85+
}
7586
}
76-
]
87+
}
7788
},
7889
"devDependencies": {
7990
"@types/node": "^20.12.2",

package.nls.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"extentions.searchFile": "vue-helper: searchFile"
2+
"extentions.blockSelect": "vue-helper: blockSelect"
33
}

package.nls.zh-CN.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"extentions.searchFile": "vue-helper: 搜索"
2+
"extentions.blockSelect": "vue-helper: 块选择"
33
}

src/assist.ts

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { commands, window, Position, TextEditor, Selection } from 'vscode'
1+
import { commands, window, Position, TextEditor, Selection, workspace, Range } from 'vscode'
22
import ExplorerProvider from './explorer'
33

44
export default class Assist {
@@ -15,6 +15,116 @@ export default class Assist {
1515
this.explorer.context.subscriptions.push(commands.registerCommand('vue-helper.funcEnhance', () => {
1616
this.funcEnhance()
1717
}))
18+
this.explorer.context.subscriptions.push(commands.registerCommand('vue-helper.backspace', () => {
19+
this.backspce()
20+
}))
21+
}
22+
23+
asNormal(key: string, modifiers?: string) {
24+
switch (key) {
25+
case 'enter':
26+
if (modifiers === 'ctrl') {
27+
return commands.executeCommand('editor.action.insertLineAfter');
28+
} else {
29+
return commands.executeCommand('type', { source: 'keyboard', text: '\n' });
30+
}
31+
case 'tab':
32+
if (workspace.getConfiguration('emmet').get<boolean>('triggerExpansionOnTab')) {
33+
return commands.executeCommand('editor.emmet.action.expandAbbreviation');
34+
} else if (modifiers === 'shift') {
35+
return commands.executeCommand('editor.action.outdentLines');
36+
} else {
37+
return commands.executeCommand('tab');
38+
}
39+
case 'backspace':
40+
return commands.executeCommand('deleteLeft');
41+
}
42+
}
43+
44+
// 回退删除处理
45+
async backspce() {
46+
let editor: any = window.activeTextEditor;
47+
if(!editor) {
48+
this.asNormal('backspace');
49+
return;
50+
}
51+
// 多选择点删除处理
52+
if(window.activeTextEditor?.selections.length && window.activeTextEditor?.selections.length > 1) {
53+
let selections = window.activeTextEditor?.selections;
54+
let selectionList: Array<Selection> = [];
55+
for (let index = 0; index < selections.length; index++) {
56+
const selection = selections[index];
57+
if(selection.start.line === selection.end.line && selection.start.character === selection.end.character) {
58+
if(selection.anchor.character > 0) {
59+
selectionList.push(new Selection(new Position(selection.anchor.line, selection.anchor.character - 1), selection.anchor));
60+
} else if (selection.anchor.line > 0) {
61+
let len = editor.document.lineAt(selection.anchor.line - 1).text.length;
62+
selectionList.push(new Selection(new Position(selection.anchor.line - 1, len), selection.anchor));
63+
}
64+
} else {
65+
selectionList.push(selection);
66+
}
67+
}
68+
await editor.edit((editBuilder: any) => {
69+
for (let i = 0; i < selectionList.length; i++) {
70+
editBuilder.delete(selectionList[i]);
71+
}
72+
});
73+
return;
74+
}
75+
if(window.activeTextEditor?.selection.start.line === window.activeTextEditor?.selection.end.line
76+
&& window.activeTextEditor?.selection.start.character === window.activeTextEditor?.selection.end.character) {
77+
// 首行
78+
if(editor.selection.anchor.line === 0) {
79+
if(editor.selection.anchor.character > 0) {
80+
await editor.edit((editBuilder: any) => {
81+
editBuilder.delete(new Selection(new Position(editor.selection.anchor.line, editor.selection.anchor.character - 1), editor.selection.anchor));
82+
});
83+
}
84+
} else {
85+
let isLineEmpty = editor.document.lineAt(editor.selection.anchor.line).text.trim() === '';
86+
// 整行都是空格
87+
if(isLineEmpty) {
88+
let preText = '';
89+
let line = editor.selection.anchor.line;
90+
while(preText.trim() === '' && line >= 0) {
91+
line -= 1;
92+
preText = editor.document.lineAt(line).text;
93+
}
94+
await editor.edit((editBuilder: any) => {
95+
editBuilder.delete(new Selection(new Position(line, preText.length), editor.selection.anchor));
96+
});
97+
} else {
98+
let startPosition: Position;
99+
let endPosition: Position = editor.selection.anchor;
100+
let preLineText = editor.document.getText(new Range(new Position(endPosition.line, 0), endPosition));
101+
if(endPosition.character === 0 || preLineText.trim() === '') {
102+
startPosition = new Position(endPosition.line - 1, editor.document.lineAt(endPosition.line - 1).text.length);
103+
} else {
104+
startPosition = new Position(endPosition.line, endPosition.character - 1);
105+
// 对{}, (), [], '', "", <>进行成对删除处理
106+
let txt = editor.document.getText(new Range(new Position(endPosition.line, endPosition.character - 1), endPosition));
107+
if(editor.document.lineAt(endPosition.line).text.length > endPosition.character) {
108+
let nextTxt = editor.document.getText(new Range(endPosition, new Position(endPosition.line, endPosition.character + 1)));
109+
if((txt === '{' && nextTxt === '}')
110+
|| (txt === '(' && nextTxt === ')')
111+
|| (txt === '\'' && nextTxt === '\'')
112+
|| (txt === '"' && nextTxt === '"')
113+
|| (txt === '[' && nextTxt === ']')
114+
|| (txt === '<' && nextTxt === '>')) {
115+
endPosition = new Position(endPosition.line, endPosition.character + 1);
116+
}
117+
}
118+
}
119+
await editor.edit((editBuilder: any) => {
120+
editBuilder.delete(new Selection(startPosition, endPosition));
121+
});
122+
}
123+
}
124+
} else {
125+
// 选择块
126+
this.asNormal('backspace');
127+
}
18128
}
19129

20130
// 函数增强

0 commit comments

Comments
 (0)