推断左/右括号?

发布于 2024-12-21 04:23:13 字数 358 浏览 4 评论 0原文

似乎有人必须努力弄清楚如何推断或建议在哪里放置右括号。我的意思是,突出显示匹配的括号很棒,但建议会更好。这是一个明确的问题吗?如果是这样,它的名字是什么(比如,我应该用谷歌学术搜索什么?)。如果没有,为什么不呢?这是一个明显不可能/提出的问题吗?

也就是说,假设我有一些格式错误的 ruby​​ 代码:

foo.all.map { |i| i.bar }).uniq.compact.reject { |j| j.match /baz/i }

请注意,我缺少最初的括号。我所说的问题是,“当我将光标移到未配对的括号上时,如何建议在开头插入括号?”

如果这不是一个不好提出的问题,为什么还没有人这样做呢?

It seems like someone must have done work on figuring out how to infer or suggest where to put closing parentheses. I mean, highlighting matching parens is great, but suggesting would be even better. Is this a defined problem? If so, what is its name (like, what should I Google Scholar?). If not, why not? Is it an obviously impossible/poorly posed question?

To wit, let's say I have some malformed, ruby code:

foo.all.map { |i| i.bar }).uniq.compact.reject { |j| j.match /baz/i }

Note that I'm missing the initial parenthesis. The problem I'm talking about is, "How do I suggest the insertion of a paren at the beginning when I move my cursor over the unpaired paren?"

If this is not a poorly posed question, why hasn't someone done this yet?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

且行且努力 2024-12-28 04:23:13

人们可以在用户键入时跟踪“平衡”与“不平衡”括号。一旦括号变得不平衡,就建议一个括号。

例如,输入 (something)) 会建议在 s 之前添加一个左括号。正确放置“修复”括号可能具有挑战性。例如,如果用户输入 something(else)) ,则有一个选择:在 g 之后或字符串开头插入修复左括号?

跟踪平衡括号相对简单:

int parenCount = 0

event keyUp(char c){
    if(c is close-paren){
        parenCount--;
    }else if(c is open-paren){
        parenCount++;
    }
    if(parenCount < 0){
        // missing an open-paren somewhere
    }
}

// handle missing close-parens when user finished typing the string...
// for example if user types "myObject.something(" and hits enter

可能还需要查找不必要的括号,如 a.doSomethingWith((aNumber))

One could keep track of "balanced" vs "unbalanced" parens as the user types. Then suggest a paren as soon as the parens become unbalanced.

For example typing (something)) would suggest an opening paren before s. Correctly placing the "fix-it" paren might be challenging. For example if the user types something(else)) there's a choice: insert the fix-it opening paren after g or at the beginning of the string?

Keeping track of balanced parens would be relatively simple:

int parenCount = 0

event keyUp(char c){
    if(c is close-paren){
        parenCount--;
    }else if(c is open-paren){
        parenCount++;
    }
    if(parenCount < 0){
        // missing an open-paren somewhere
    }
}

// handle missing close-parens when user finished typing the string...
// for example if user types "myObject.something(" and hits enter

Might also need to look for unnecessary parens as in a.doSomethingWith((aNumber))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文