Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Syntax coloring

KamaShinKamaShin Posts: 2Member
Hi everyone,
here is my problem:
I'm writing a script editor for a certain language and would like my editor to have syntax coloring... I already came up with an algorithm to do so but there are 2 BIIIIG problems:
1) it works only 90% of the time, not 100%
2) when the file contains more than 100 lines of code, it becomes real slow. Since the syntax coloring method is called each time the user press a key in the editor, it s very tiresome to add a letter, wait for 15 sec, then add another letter and so on...

so if anyone can tell me where I can find good algorithms or give me some hints about how such a thing is supposed to be coded that would be GREAT :)

(My current algorithm works basically that way: I have an array storing about 388 keywords... my method, for each of these 388 keywords look for all occurences of them AND verify that each time a keyword is found, it s a whole word (for example, if "END" is a keyword, it will be found in the following phrase: "durENDal" however it must not be put in blue cause it's not a whole word)... after all the 388 keywords have been processed, It deals with strings, then with comments, and finally with numbers... this is not very efficient)

Comments

  • DrMartenDrMarten Posts: 748Member
    : Hi everyone,
    : here is my problem:
    : I'm writing a script editor for a certain language and would like my editor to have syntax coloring... I already came up with an algorithm to do so but there are 2 BIIIIG problems:
    : 1) it works only 90% of the time, not 100%
    : 2) when the file contains more than 100 lines of code, it becomes real slow. Since the syntax coloring method is called each time the user press a key in the editor, it s very tiresome to add a letter, wait for 15 sec, then add another letter and so on...
    :
    : so if anyone can tell me where I can find good algorithms or give me some hints about how such a thing is supposed to be coded that would be GREAT :)
    :
    : (My current algorithm works basically that way: I have an array storing about 388 keywords... my method, for each of these 388 keywords look for all occurences of them AND verify that each time a keyword is found, it s a whole word (for example, if "END" is a keyword, it will be found in the following phrase: "durENDal" however it must not be put in blue cause it's not a whole word)... after all the 388 keywords have been processed, It deals with strings, then with comments, and finally with numbers... this is not very efficient)
    :



    For better "EFFICIENCY" try to do ALL your checks in just ONE LOOP or reduce the number of loops.
    Then your code will be quicker.

    Hope this helps. :-)


  • jerubaaljerubaal Posts: 19Member
    I'm not sure if I'm reading you right, but it seems that you have written an algo that goes through the entire code and checks all of the text for syntax coloring each time. Just make the algorithm look at the line that is currently being worked on. That should make it easier. The only problem I can see from the method I have proposed is if a person pastes code from somewhere else. All you need to do is keep track of which lines experience change and have the editor affect those only. Also, you could make an option to toggle syntax highlighting for users who think it goes too slow.
    1337 d00d

Sign In or Register to comment.