编程文本编辑器加入花括号 [英] Programming text editor joining curly braces

查看:132
本文介绍了编程文本编辑器加入花括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在编写自己的JS,基于行的编程文本编辑器。一切都很顺利,但我坚持使用一个功能,你在一些编辑器中看到的图形线,它连接{}以形成一个代码块。我上传了一个硬编码版本的屏幕截图,其中包含第2-7行此处 [ ^ ]。



我遇到的问题是找出哪个线需要链接。也就是说,解析代码,选择{}并以某种方式决定哪一个形成一对。



这将必须处理包括错误的一切在内的所有可能性,例如as;



如果{和}现在处于行首,我只会绘制图形,实现对函数x(){稍后。



Hi,

I'm working on writing my own JS, line-based programming text editor. Everything's going really well but I'm stuck on one feature, the graphical line you see in some editors which joins {} to form a code block. I've uploaded a screenshot of a hard-coded version linking lines 2-7 here[^].

What I'm having trouble with is figuring out which lines need to be linked. That is, parsing the code, picking out the {} and somehow deciding which ones form a pair.

This will have to deal with every possibility including erroneous ones, such as;

I'll only draw the graphic if the { and } are at the start of the line for now, implementing support for things like function x() { later on.

{
}
---
{

}
---
{
    {
}
---
{
    }
}
---
{
    {

    }
}
---
{
}

{
}





我只是喜欢一些关于最佳方法的建议请



我尝试过:



-------------------------------------------------- ------------------



I'd just like some advice on the best approach please

What I have tried:

--------------------------------------------------------------------

推荐答案

首先确保所有内容都是配对(开启和关闭大括号计数必须相同。



然后反向读取到配对括号。



读取反向以找到关闭括号;匹配的开口大括号是包括任何儿童对的大括号。



通过反复计算开启和关闭大括号,你可以匹配哪个大括号哪个;你甚至可以确定你所处的等级。
First make sure everything is "paired" (the opening and closing "brace counts" must be the same).

Then "read reverse" to "pair" braces.

Read reverse to find a "closing" brace; the matching "opening" brace is the one that includes any "child pairs" of braces.

By "counting" opening and closing braces as you read in reverse, you can match which brace goes with which; you can even determine what "level" you're at.


这篇关于编程文本编辑器加入花括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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