所以我试图制作一个视觉工作室代码扩展,基本上抓取当前行值并解析它并将px转换为rem,(我已经将这些变量设置为稍后我希望修改基数和单位)
我在Microsoft API网站上看到的就是如何获得突出显示的值,所以我首先考虑了这一点,因为我想我想让函数先运行。
然后使用我的代码,我不确定如果该行超过1值,如何将其作为最终值返回,例如,
保证金:22px 32px 0 32px;
以下是代码。
'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);
}
答案 0 :(得分:1)
您可以使用选择对象,如下所示:
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);