簡體   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