So I am trying to make a visual studio code extension that basically grabs the current lines value and parses it and turns px to rem, (I have made these variables as later I wish to modify the base and units)
All I can see on the Microsoft API site is how to get the highlighted value, So I went with this first as I figured I wanted to just get the function working first.
Then with my code I am unsure on how to return it as a final value if the line is more than 1 value eg,
margin: 22px 32px 0 32px;
Below is the code.
'use strict';
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
let disposable = vscode.commands.registerCommand('extension.pxToEm', () => {
var base, editor, initBase, original, selection, text, unit, values, totalBase, position;
editor = vscode.window.activeTextEditor;
selection = editor.selection;
text = editor.document.getText(selection);
base = <any>16;
unit = 'rem';
values = text.match(/([0-9]+)px/gi);
var returnValue = function(text) {
if (values !== null) {
values.forEach(function(val, key) {
text = text.replace(val, parseInt(val) / base + unit);
if (key > values.length - 1) {
totalBase = '/' + base.replace(/(\r\n|\n|\r)/gi, '');
text = text.replace(totalBase, ' ').replace(/(\r\n|\n|\r)/gi, '');
text = text + '\n';
}
});
}
return text;
};
vscode.window.showInformationMessage(returnValue(text));
});
context.subscriptions.push(disposable);
}
You could use the selection object, like this:
editor = vscode.window.activeTextEditor;
selection = editor.selection;
if(selection.isEmpty)
{
// the Position object gives you the line and character where the cursor is
const position = editor.selection.active;
var newPosition = position.with(position.line, 0);
var newSelection = new vscode.Selection(newPosition, newPosition);
editor.selection = newSelection;
}
text = editor.document.getText(selection);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.