Create a VS Code Plugin.

at first

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.

policy

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.

Coded

Character → coding was realized with the following feeling.

  1. Get the file string from the document.
  2. Repeat line by line to get Japanese as a regular expression and create an array.
  3. Create a code from the array created in 2., and create an associative array of Japanese and code.
  4. Replace with the replace method on the original line.
  5. Create a new document by collecting the replaced strings.
  6. Documents in the editor 5. Replace with the document created in.

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);
        }
    });
}

reverse

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;
            }
        }
    });
}

Create a VSIX file with VSCE.

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.

Summary

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

Create a VS Code Plugin.
VS Code plugin recommended for programming school students
Run a Spring Boot project in VS Code
Make a snippet for Thymeleaf in VS Code
Create Spring Boot environment with Windows + VS Code
Try debugging a Java program with VS Code
Build a Java development environment with VS Code
[Java] Create a filter
How to display a browser preview in VS Code
[Environment construction] Build a Java development environment with VS Code!
Beginners create Spring Tools Suite environment with VS Code
Create a Java (Gradle) project with VS Code and develop it on a Docker container
Create a private key / public key in CentOS8.2 and connect to SSH with VS Code
Create a Java (Maven) project with VS Code and develop it on a Docker container
Run VS Code on Docker
Docker management with VS Code
Format Ruby with VS Code
Create a java method [Memo] [java11]
Hello World with VS Code!
[Java] Create a temporary file
Create a playground with Xcode 12
Create a fortune using Ruby
A memo to start Java programming with VS Code (2020-04 version)
Create a MOB using the Minecraft Java Mythicmobs plugin | Preparation 1
How to create a method
Create a name input function
[Be careful about changing the version of Xdebug! ] Create a development environment with Xdebug3 + docker + VS Code
[Note] A story about changing Java build tools with VS Code
Building a haskell environment with Docker + VS Code on Windows 10 Home
How to open a script file from Ubuntu with VS code
[Swift] Create UIButton class in code
Create a Vue3 environment with Docker!
[Android] Create a calendar using GridView
Create a Jetty project using Eclipse
A memorandum for writing beautiful code
Create a lightweight STNS Docker image
Preparing to create a Rails application
Java Spring environment in vs Code
[Rails Tutorial Chapter 5] Create a layout
Java 15 implementation and VS Code preferences
Create a database in a production environment
Create a new app in Rails
Create a Java project using Eclipse
[Java] How to create a folder
Java build with mac vs code
Create a filtering function using acts-as-taggable-on
Create a Servlet program in Eclipse
How to make a Jenkins plugin
Try to create a server-client app
Switch from Eclipse to VS Code
Getting Started with Docker with VS Code
Java development environment (Mac, VS Code)
Create exceptions with a fluid interface
[Kotlin / Android] Create a custom view
Create a Maven project with a command
Create a fluentd server for testing
A memorandum when IME cannot be turned on with VS Code (Ubuntu 20.04)
A memo that enabled VS Code + JUnit 5 to be used on Windows 10