In the configuration file (Properties file) of Java9 and earlier applications, Japanese (2-byte code) cannot be used as it is, and it was necessary to replace it with a code starting with'\ u'. I was in a position to maintain pre-Java 9 applications, and it was a daunting and tedious task to do native2ascii every time I used VS Code.
Therefore, I decided to create a function equivalent to native2ascii with the VScode plugin.
First, create an environment for developing VS Code plugins. How to create an environment is "Visual Studio Code First Extension Development" by @rma. Please refer to the.
As far as I could see, I couldn't kick the command on VSCode or pick up the "read Properties file" event (please give me some information), so I manually used the VSCode command. We proceeded with the policy of converting (character → coding) and reverse (code → character) with.
Character → coding was realized with the following feeling.
In addition, surrogate characters are not supported.
function ascii(e: vscode.TextEditor, d: vscode.TextDocument, sel: vscode.Selection[]) {
console.log(d.getText());
let text = d.getText();
let newTest = text.split("\n");
let replaced = "";
newTest.forEach(val => {
let reg = val.match(/([^\x01-\x7E])/g);
if (reg) {
let t = reg.map(code => {
let unicode = code.charCodeAt(0).toString(16);
return {code, value:`\\u${unicode}`};
}).reduce( (v,obj) => {
let {code, value} = obj;
let reg = new RegExp(code);
return v.replace(reg, value);
}, val);
replaced = `${replaced}${t}\n`;
} else {
replaced = `${replaced}${val}\n`;
}
});
e.edit((builder)=>{
if (e) {
let startPos = e.document.positionAt(0);
let endPos = e.document.positionAt(text.length);
let allRange = new vscode.Range(startPos,endPos);
builder.replace(allRange,replaced);
}
});
}
It's not much different from the coded one. I am creating characters from Unicode.
function reverse(e: vscode.TextEditor, d: vscode.TextDocument, sel: vscode.Selection[]) {
console.log(d.getText());
let text = d.getText();
let newTest = text.split("\n");
let replaced = "";
newTest.forEach(val => {
let reg = val.match(/(\\u[0-9|a-f]{4})/g);
if (reg) {
let t = reg.map(unicode => {
let codeStrs = unicode.split("\\u");
let codePoints = parseInt(codeStrs[1], 16);
return {code:`\\${unicode}`, value:String.fromCharCode(codePoints)};
}).reduce( (v,obj) => {
let {code, value} = obj;
let reg = new RegExp(code);
return v.replace(reg, value);
}, val);
replaced = `${replaced}${t}\n`;
} else {
replaced = `${replaced}${val}\n`;
}
});
e.edit((builder)=>{
if (e) {
let startPos = e.document.positionAt(0);
let endPos = e.document.positionAt(text.length);
let allRange = new vscode.Range(startPos,endPos);
builder.replace(allRange,replaced);
}
});
}
native2ascii I created the following functions and summarized them so that the functions created above can be called by commands. Call this function with registerCommand and you're done.
function native2ascii() {
if (!vscode.window.activeTextEditor) {
vscode.window.showInformationMessage('Open a file first to manipulate text selections');
return;
}
var items: vscode.QuickPickItem[] = [];
items.push({ label: "ascii", description: "Convert from character to Unicode" });
items.push({ label: "reverse", description: "Convert from Unicode to characters" });
Window.showQuickPick(items).then((selection) => {
if (!selection) {
return;
}
let e = Window.activeTextEditor;
if (e) {
let d = e.document;
let sel = e.selections;
switch (selection.label) {
case "ascii":
ascii(e, d, sel);
break;
case "reverse":
reverse(e, d, sel);
break;
default:
console.log("hum this should not have happend - no selection")
break;
}
}
});
}
Package the created plugin. I used VSCE. I installed it with the following command.
npm install -g vsce
After installation, package it with the following command. If you do not rewrite README.md, packaging will fail.
vsce package
The source code is saved in GitHub.
The environment for making plugins was substantial, and there was no place to get caught (thanks to @rma). It was my first time to use typescript, but since I was touching ES2015, I didn't feel any discomfort. Rather, VScode has good language support and I wanted to use typescript in earnest.
Recommended Posts