Skip to content

Commit

Permalink
将9和0放到按键映射中,避免重复调用的大问题
Browse files Browse the repository at this point in the history
  • Loading branch information
iwxyi committed Oct 20, 2019
1 parent ee5af0b commit 8ce6859
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 183 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ All notable changes to the "lazykey" extension will be documented in this file.
## [v0.0.2]

- Support `JavaSript`
- Add `Enter` key with skipping right parentheses and auto indentation
- Add smarter `Enter` key with ignoring right parentheses and auto indentation
- Add smarter `Tab` key with skipping and inserting in a parameter list
- Hide suggest after point `->` if exists variable on the right
- Repair the case of `cout < <`
- Support more case like converting `qDebug(),,` to `qDebug() << `
- Auto detect whether `< >` or `" "` to be used after `#include` by space key
- May fixed converting `9` to `()` twice
- Fix putting the current line in the code block bt `[`
- Add `{ }` at the end of the line beginning with `if/for/...`
- Auto indent when inserting `{ }` in a blank line

## [v0.0.1]

Expand Down
3 changes: 2 additions & 1 deletion extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ function activate(context) {

// ProvideCompletion
require('./src/key_dot.js')(context);
require('./src/key_nine.js')(context);
require('./src/key_zero.js')(context);
require('./src/key_space.js')(context);
require('./src/key_minus.js')(context);
Expand All @@ -32,6 +31,8 @@ function activate(context) {
// KeyBindings
context.subscriptions.push(vscode.commands.registerCommand('extension.keybindings_enter', require('./src/bind_enter.js')));
context.subscriptions.push(vscode.commands.registerCommand('extension.keybindings_tab', require('./src/bind_tab.js')));
context.subscriptions.push(vscode.commands.registerCommand('extension.keybindings_nine', require('./src/bind_nine.js')));
context.subscriptions.push(vscode.commands.registerCommand('extension.keybindings_zero', require('./src/bind_zero.js')));
}
exports.activate = activate;

Expand Down
92 changes: 92 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@
{
"command": "extension.keybindings_tab",
"title": "KeyBindings Tab"
},
{
"command": "extension.keybindings_nine",
"title": "KeyBindings Nine"
},
{
"command": "extension.keybindings_zero",
"title": "KeyBindings Zero"
}
],
"keybindings": [
Expand Down Expand Up @@ -165,11 +173,95 @@
"mac": "enter",
"when": "editorTextFocus && !suggestWidgetVisible && editorLangId==cs"
},
{
"command": "extension.keybindings_tab",
"key": "tab",
"mac": "tab",
"when": "editorTextFocus && !suggestWidgetVisible && editorLangId==c"
},
{
"command": "extension.keybindings_tab",
"key": "tab",
"mac": "tab",
"when": "editorTextFocus && !suggestWidgetVisible && editorLangId==cpp"
},
{
"command": "extension.keybindings_tab",
"key": "tab",
"mac": "tab",
"when": "editorTextFocus && !suggestWidgetVisible && editorLangId==java"
},
{
"command": "extension.keybindings_tab",
"key": "tab",
"mac": "tab",
"when": "editorTextFocus && !suggestWidgetVisible && editorLangId==js"
},
{
"command": "extension.keybindings_tab",
"key": "tab",
"mac": "tab",
"when": "editorTextFocus && !suggestWidgetVisible && editorLangId==javascript"
},
{
"command": "extension.keybindings_tab",
"key": "tab",
"mac": "tab",
"when": "editorTextFocus && !suggestWidgetVisible && editorLangId==php"
},
{
"command": "extension.keybindings_tab",
"key": "tab",
"mac": "tab",
"when": "editorTextFocus && !suggestWidgetVisible && editorLangId==cs"
},
{
"command": "extension.keybindings_nine",
"key": "9",
"mac": "9",
"when": "editorTextFocus && editorLangId==c"
},
{
"command": "extension.keybindings_nine",
"key": "9",
"mac": "9",
"when": "editorTextFocus && editorLangId==cpp"
},
{
"command": "extension.keybindings_nine",
"key": "9",
"mac": "9",
"when": "editorTextFocus && editorLangId==java"
},
{
"command": "extension.keybindings_nine",
"key": "9",
"mac": "9",
"when": "editorTextFocus && editorLangId==js"
},
{
"command": "extension.keybindings_nine",
"key": "9",
"mac": "9",
"when": "editorTextFocus && editorLangId==javascript"
},
{
"command": "extension.keybindings_nine",
"key": "9",
"mac": "9",
"when": "editorTextFocus && editorLangId==php"
},
{
"command": "extension.keybindings_nine",
"key": "9",
"mac": "9",
"when": "editorTextFocus && editorLangId==cs"
},
{
"command": "extension.keybindings_zero",
"key": "0",
"mac": "0",
"when": "editorTextFocus && editorLangId==cpp"
}
]
},
Expand Down
101 changes: 101 additions & 0 deletions src/bind_nine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* 9 转 ()
*/


const vscode = require('vscode');

function processNine() {
// 读取设置是否开启
if (!vscode.workspace.getConfiguration().get('LazyKey.NumberToParentheses')) {
normalNine();
return;
}

const editor = vscode.window.activeTextEditor;
if (editor.selection.text != undefined) return; // 有选中文本了

const document = editor.document;
const selection = editor.selection;
if (selection.start.line != selection.end.line || selection.start.character != selection.end.character) {
return normalNine();
}
var position = selection.active;

if (!analyzeNine(editor, document, position))
normalNine();
}

/**
* 判断需不需要跳过右边的内容
*/
function analyzeNine(editor, document, position) {
// 获取全文和当前行内容
var full = document.getText();
var word = document.getText(document.getWordRangeAtPosition(position)); // 点号左边的单词
var line = document.lineAt(position).text;
var left = line.substring(0, position.character);
var right = line.substring(position.character);

// 不处理连续数字
if (/\d+$/.test(left) || /^\d+/.test(right))
return false;

// 判断各种情况是 9 还是 (
// 判断 单词9 的情况。有一次会匹配到自身,所以至少需要匹配两次
if (/[\w_]+$/.test(left) && full.match(new RegExp("\\b" + word + '9', 'g')) != null)
return false;

// 光标左右的左右括号的数量
var ll = 0, lr = 0, rl = 0, rr = 0;
for (var j = 0; j < left.length; j++) {
var c = left.charAt(j);
if (c == '(')
ll++;
else if (c == ')')
lr++;
}
for (var j = 0; j < right.length; j++) {
var c = right.charAt(j);
if (c == '(')
rl++;
else if (c == ')')
rr++;
}

// 使用队列判断需不需要添加左括号
// 左括号数量<=右括号数量
// ((|(() // 这是什么鬼啊
if (ll + rl > lr + rr && rl > rr)
return false;

// 使用栈判断需不需要添加右括号
// 左括号数量<=右括号数量
// ((|)))
var withRight = !(ll + rl < lr + rr && rl < rr);
var tabRight = vscode.workspace.getConfiguration().get('LazyKey.TabSkipRightParenthese');
var newText = withRight ?
(tabRight ? "($1)" : "($0)")
: "(";

vscode.commands.executeCommand('editor.action.insertSnippet', { 'snippet': newText });

// 延时出现提示(必须延时才会出现)
if (vscode.workspace.getConfiguration().get('LazyKey.AutoSuggestion')) {
setTimeout(function () {
vscode.commands.executeCommand('editor.action.triggerSuggest');
}, 100);
}

return true;
}

/**
* 仅仅添加一行
*/
function normalNine() {
vscode.commands.executeCommand('editor.action.insertSnippet', { 'snippet': '9'});
}


module.exports = processNine;
4 changes: 3 additions & 1 deletion src/bind_tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ function analyzeSkip(editor, document, position) {

vscode.commands.executeCommand('editor.action.insertLineAfter');
}
else {
return false;
}
}
else {
return false;
}


return true;
}

Expand Down
128 changes: 128 additions & 0 deletions src/bind_zero.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* 0 转 )
*/


const vscode = require('vscode');

function processZero() {
// 读取设置是否开启
if (!vscode.workspace.getConfiguration().get('LazyKey.NumberToParentheses')) {
normalZero();
return;
}

const editor = vscode.window.activeTextEditor;
if (editor.selection.text != undefined) return; // 有选中文本了

const document = editor.document;
const selection = editor.selection;
if (selection.start.line != selection.end.line || selection.start.character != selection.end.character) {
return normalZero();
}
var position = selection.active;

if (!analyzeZero(editor, document, position))
normalZero();
}

/**
* 判断需不需要跳过右边的内容
*/
function analyzeZero(editor, document, position) {
// 获取全文和当前行内容
var full = document.getText();
var word = document.getText(document.getWordRangeAtPosition(position)); // 点号左边的单词
var line = document.lineAt(position).text;
var left = line.substring(0, position.character);
var right = line.substring(position.character);

// 倒序遍历每一个光标
// 多个,有一个需要添加则进行添加
var selections = editor.selections;
let textEdits = [];
var isAllSkip = true; // 是否全部跳过。有一个需要添加的则进行添加
var canSkipIfAllSkip = true;
for (var i = selections.length - 1; i >= 0; --i) {
// 获取全文和当前行内容
position = selections[i].end;
var full = document.getText();
var word = document.getText(document.getWordRangeAtPosition(position)); // 左边的单词
var line = document.lineAt(position).text;
var left = line.substring(0, position.character);
var right = line.substring(position.character);

// 不处理连续数字
if (/\D$/.test(left) && right.startsWith(')')) // 排除单纯一个0并右括号结束,例如:at(0|)、at(a, 0|)
;
else if (/\d+$/.test(left) || /^\d+/.test(right))
return;

// 判断是插入 0 还是插入 )
// 左边是自增/自减,很可能是右括号
if (/(\+\+|\-\-)$/.test(left))
;
// 左边是空白或者运算符,很可能是 0
else if (/[ =\+\-*\/%\.<>]$/.test(left))
return;
// 一些函数后面必定有参数的,也是 0
else if (/(at|insert|of|remove|add|set\w+)\($/.test(left) && right.startsWith(')'))
return;
// 判断 单词0 是否存在
else if (/[\w_]+$/.test(left) && full.match(new RegExp("\\b" + word + "0", 'g')) != null)
return;

// 光标左右的左右括号的数量
var ll = 0, lr = 0, rl = 0, rr = 0;
for (let c of left) {
if (c == '(')
ll++;
else if (c == ')')
lr++;
}
for (let c of right) {
if (c == '(')
rl++;
else if (c == ')')
rr++;
}

// 如果左右括号已经匹配了,则跳过
var isSkip = (ll + rl <= lr + rr);
if (!isSkip) { // 有添加的部分,则执行添加
isAllSkip = false;
} else if (right.length == 0 || right.substring(0, 1) != ")") {
canSkipIfAllSkip = false;
}

var newText = isSkip ? "" : ")";

// 点号的位置替换为指针
var newEdit = vscode.TextEdit.insert(position, newText);

// 添加本次的修改
textEdits.push(newEdit);
}

// 应用到编辑器
if (!isAllSkip) { // 只要有需要添加的,就进行添加
let wordspaceEdit = new vscode.WorkspaceEdit();
wordspaceEdit.set(document.uri, textEdits);
vscode.workspace.applyEdit(wordspaceEdit);
} else if (canSkipIfAllSkip) { // 全部跳过,并且是全能跳过的,光标右移1
// 光标右移 1
vscode.commands.executeCommand('cursorRight');
}

return true;
}

/**
* 仅仅添加一行
*/
function normalZero() {
vscode.commands.executeCommand('editor.action.insertSnippet', { 'snippet': '0' });
}


module.exports = processZero;
Loading

0 comments on commit 8ce6859

Please sign in to comment.