Created
April 26, 2020 06:38
-
-
Save nobodxbodon/a0f289bab48aa481f6e568d0397609d4 to your computer and use it in GitHub Desktop.
vs code插件 auto-correct 基础上实现了中文标点符号自动替换为英文标点符号的原型
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import * as vscode from 'vscode'; | |
import { getWords } from './helpers'; | |
const ks = require('node-key-sender'); | |
// let config: vscode.WorkspaceConfiguration; | |
let words: any; | |
// let triggers: string[]; | |
export function activate(context: vscode.ExtensionContext) { | |
words = words || getWords(); | |
vscode.workspace.onDidOpenTextDocument(() => { | |
words = words || getWords(); | |
}); | |
vscode.workspace.onDidChangeTextDocument(event => { | |
words = words || getWords(); | |
correctTheWord(event); | |
}); | |
// vscode.workspace.onDidChangeConfiguration(e => { | |
// if (e.affectsConfiguration('auto-correct')) { | |
// config = getConfig(); | |
// } | |
// }); | |
} | |
// this method is called when your extension is deactivated | |
export function deactivate() {} | |
function correctTheWord(event: vscode.TextDocumentChangeEvent): void { | |
if (!event.contentChanges.length) { | |
return; | |
} | |
console.log(event.contentChanges[0].text); | |
let lastChr = event.contentChanges[0].text; | |
// if (!!event.contentChanges[0].text.match(/[A-Za-z]/)) { | |
// return; | |
// } | |
const editor = vscode.window.activeTextEditor; | |
if (!editor) { | |
return; | |
} | |
// const { selection } = editor; | |
// const originalPosition = selection.start.translate(0, 1); | |
// const startPos = new vscode.Position(0, 0); | |
// const text = editor.document.getText( | |
// new vscode.Range(startPos, originalPosition) | |
// ); | |
if (lastChr == '。') { | |
// console.log("hah"); | |
editor.edit( | |
editBuilder => { | |
const contentChangeRange = event.contentChanges[0].range; | |
const startLine = contentChangeRange.start.line; | |
const startCharacter = contentChangeRange.start.character; | |
const start = new vscode.Position(startLine, startCharacter); | |
const end = new vscode.Position( | |
startLine, | |
startCharacter + 1 | |
); | |
editBuilder.delete(new vscode.Range(start, end)); | |
// editBuilder.insert(start, ','); | |
// editBuilder.replace(new vscode.Range(start, end), '.'); | |
}, | |
{ | |
undoStopAfter: false, | |
undoStopBefore: false, | |
} | |
); | |
ks.setOption('globalDelayPressMillisec', 1000); | |
// setTimeout(() => { | |
// ks.sendKey('shift'); | |
// ks.sendLetter('.'); | |
// ks.sendKey('shift'); | |
ks.startBatch() | |
.batchTypeKey('shift') | |
.batchTypeKey('period') | |
.batchTypeKey('shift') | |
.sendBatch(); | |
// }, 100); | |
} | |
/* | |
// matches letters and special letters | |
const re = /(\p{L}+)[\W]?$/gu; | |
const match = re.exec(text); | |
const lastWord = match && match.length > 1 && match[1]; | |
// if (triggers.length) { | |
// const lastTyped = text.substr(-1); | |
// if (!triggers.includes(lastTyped)) { | |
// return; | |
// } | |
// } | |
if (!lastWord) { | |
return; | |
} | |
const keys = Object.keys(words); | |
if (keys.includes(lastWord)) { | |
editor.edit( | |
editBuilder => { | |
const contentChangeRange = event.contentChanges[0].range; | |
const startLine = contentChangeRange.start.line; | |
const startCharacter = contentChangeRange.start.character; | |
const start = new vscode.Position(startLine, startCharacter); | |
const end = new vscode.Position( | |
startLine, | |
startCharacter - lastWord.length | |
); | |
editBuilder.delete(new vscode.Range(start, end)); | |
editBuilder.insert(start, words[lastWord]); | |
}, | |
{ | |
undoStopAfter: false, | |
undoStopBefore: false, | |
} | |
); | |
}*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment