浏览器中的Microsoft Monaco Editor获得价值所在 [英] Microsoft Monaco Editor in browser get value of line

查看:85
本文介绍了浏览器中的Microsoft Monaco Editor获得价值所在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用基于浏览器的Microsoft Monaco Editor版本,在操场上找不到任何文档或示例来告诉您如何在编辑器中获取特定行的值.

I have been working through using the browser based version of the Microsoft Monaco Editor, I can't find any documentation or any examples in the playground that tell you how to get the value of a specific line of in the editor.

例如:

class Example {
    private m:number;

    public met(): string {
        return "Hello world!";
    }
}

第2行应为 private m:number; .

如何获取该行甚至行的一部分的值,然后使用代码更改其值.我想将该操作放入键盘快捷键.

How would you get the value of that line or even part of the line and then change it's value using code. I want to put that action into a keyboard shortcut.

推荐答案

我认为摩纳哥没有这种内置功能,因为我没有找到它.但我使用以下代码来做到这一点:

I think there is no such built-in feature in monaco as I do not found it. but i am doing that using following code:

editor.addAction({
        id: 'some_id',
        label: 'label',
        keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_C],
        run: function(ed) {
            var position = ed.getPosition();
            var text = ed.getValue(position);
            var splitedText=text.split("\n");
            var line = splitedText[position.lineNumber-1];

            // now you have current line
            // you can also get any other line
            // and do something with that line

            splitedText[position.lineNumber-1]= _newLineText_
            ed.setValue(splitedText.join("\n"));
            ed.setPosition(position); // to return the pointer to the a position before editing text

            return null;
        },
        enablement: {
            textFocus: true,
        }
    });

此方法对小文件有用,但对于大文件,整个编辑器将重新突出显示,这是一件坏事.

this method is good for small file but for large file the whole editor will re highlights and that a bad thing.

这篇关于浏览器中的Microsoft Monaco Editor获得价值所在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆