【问题标题】:Trying to make Visual Studio Code Extension, 3 questions尝试制作 Visual Studio Code Extension,3 个问题
【发布时间】:2016-11-03 11:28:58
【问题描述】:

所以我正在尝试制作一个 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);
    }

【问题讨论】:

  • 我已经更新了我的代码,它现在可以正常工作但是我只是希望通过光标当前行来使用它。我不知道。任何帮助将不胜感激(所以从当前行而不是选择中获取内容,并在返回时替换它

标签: javascript typescript visual-studio-code vscode-extensions


【解决方案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);

【讨论】:

    猜你喜欢
    • 2020-02-04
    • 2022-12-02
    • 2017-09-14
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 2022-01-24
    • 1970-01-01
    相关资源
    最近更新 更多