Page 1 of 2

creating an ide

Posted: Fri Jan 09, 2015 10:20 am
by Doctory
hello everyone :)
i was wondering if it was possible to create an ide using lua and love2d.
i would like some of the following things:

-syntax highlighting for lua + love2d
-project tree
-saving and loading
-(edit) getting the current open file

how would i do that? im really interested in syntax highlighting and saving and loading, the project tree isnt neccessary but it would be good if you could explain that as well.

thanks ^^

Re: creating an ide

Posted: Fri Jan 09, 2015 10:47 am
by ivan
Sure, with a lot of work it is possible but in IMO it would not be very useful.
For starters, you would need a stable and robust "textarea"-like UI object.
Apart from the UI programming, there are many little things like handling various file encodings, copy/paste via the clipboard, keyboard input layouts and so on.
For syntax highlighting, you would need a simple Lua language parser.
All of the features you mentioned are available in Notepad++.

Re: creating an ide

Posted: Fri Jan 09, 2015 11:16 am
by kikito
Also last time I checked LÖVE was not exactly super fast when rendering text, which could give you trouble (last time I checked this was some time ago, so it might have changed).

If you are interested in using Lua to code an editor, you could give a look at textadept. For full-fledged IDEs, Zerobrane Studio is worth a look. I don't know the exact percentage of Lua code vs native code on these projects, but I've heard it's significant. neovim is also worth noting out; one of its aims is replacing VimL in vim with Lua.

Re: creating an ide

Posted: Fri Jan 09, 2015 11:52 am
by Doctory
i dont need an ide, i got one already, but im intersted in making one.

Re: creating an ide

Posted: Fri Jan 09, 2015 12:12 pm
by kikito
I was not pointing to those so you could use them. I was pointing to them so you could look at their source code and learn from them - to scratch that itch.

Re: creating an ide

Posted: Fri Jan 09, 2015 12:31 pm
by Doctory
kikito wrote:I was not pointing to those so you could use them. I was pointing to them so you could look at their source code and learn from them - to scratch that itch.
ah, thanks.

Re: creating an ide

Posted: Fri Jan 09, 2015 6:36 pm
by rok
@kikito Thanks for mentioning textadept. Never heard of it before. Looks great :awesome:

Re: creating an ide

Posted: Sat Jan 10, 2015 3:56 am
by Positive07
I was considering this idea, my conclusion:

The worst part is the text area control... why? Well textareas have quite a bunch of features:
  1. Cursor:
    • To move the cursor when the the user click you need to get in which character of the text the cursor is. To do this you need to calculate the line where the user clicked (this can be done with the y coordinate of the mouse and the line height), then calculate the character position (with something like Font:getWrap and the x coordinate of the mouse)
    • The cursor also moves with the arrows keys and whenever a character is added. This is easy, just increment or decrement the pointer
    • Characters are added at the cursor position, you can do this with string.sub and concatenation
    • Pressing the up or down arrow keys and moving up to a similar position in the line above, this is really hard too, maybe with a method similar to the mouse cursor.
  2. Selecting text:
    • This is the hardest, dragging the mouse while the left click is pressed, selects a bunch of text, to do this you can get the first cursor with the same method described above, and then have a second cursor which is dynamic (while the mouse is still pressed) and after the click is released get the selection.
    • Selections are also modified when the shift key is pressed and an arrow is pressed.
    • If you move to the left or right the selection is unselected.
    • When you press a key, the selection gets replaced.
    • forget about complicated things like tabulating the selection when tab is pressed or such.
Followed by this you have syntax highlighting, to do this, you need a parser as said by Ivan, this should parse the whole document and split the different zones, lets said you have this text:

Code: Select all

function    (a)
    a = {"Hello",123}
end
The parser should return a table like:

Code: Select all

line[1]={
    {"function",type="function"};
    {"    "};
    {"(",type="operator"}
    {"a",type="none"}
    {")",type="operator"}
}
line[2]={
    {"    a ",type="none"}
    {"= ",type="operator"};
    {"{",type="table"};
    {"\"Hello\"",type="string"}
    {",",type="operator"}
    {"123",type="number"}
    {"}",type="table"}
}
line[3]={
    {"end",type="function"}
}
There are many of this things, an example is LPeg which I dont like and needs a C library to work, but is commonly used for this tasks.

You dont need to parse the file on every update, since most of it will be unchanged, just parse the lines that where modified when a character is input or a selection is deleted. But again, you dont need to parse the whole file, just the part that changed

Then you need to interpret the parser output and print it to the screen, with the appropriate fonts and colors. Also you need to know how much width each word uses so that they are correctly separated. For that you could turn that table into something a library like Robyn's richtext library understands, and leave the printing work to it.

I may suggest you frustum-cull this text, because as Kikito said, printing to the screen is an expensive function when doing it so many times, so you will need to know which parts of the text should be displayed and which ones are not needed so you dont waste time printing unseen text.

You will also need a scrollbar. That was the first thing I did, because it is lot of math, It is in one of my repositories (Fruit), you can use it if you want

Copying and pasting was made easier with the new love.system functions ([g/s]etClipboard()) so there is nothing to worry over there.

Re: creating an ide

Posted: Sat Jan 10, 2015 12:16 pm
by Robin
Positive07 wrote:Robyn
:huh:

:P

Re: creating an ide

Posted: Sat Jan 10, 2015 12:51 pm
by Positive07
Dont know how I could make that mistake... I may have been asleep hahaha... I'm sure I wrote Robin!!