繁体   English   中英

尝试制作Visual Studio代码扩展,三个问题

[英]Trying to make Visual Studio Code Extension, 3 questions

因此,我尝试进行一个Visual Studio代码扩展,该扩展基本上可以获取当前行的值并将其解析并将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);
    }

您可以使用选择对象,如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM