# CodeEditorComponent

**URL:** <https://forum.juce.com/t/codeeditorcomponent/4466>\
**Category:** General JUCE discussion\
**Created:** [November 3, 2009, 7:01pm UTC](https://forum.juce.com/t/codeeditorcomponent/4466 "2009-11-03T19:01:35Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 3, 2009, 7:01pm UTC](https://forum.juce.com/t/codeeditorcomponent/4466/1 "2009-11-03T19:01:35Z")

</div>

FYI I’ve just checked-in an extremely “alpha” version of some source code editing classes.

I mentioned this a while back and a few people expressed an interest, so feel free to have a look and see what you think of the approach I’ve taken.

Obviously it’s very fresh code, and I’ve not even added a demo for it yet, but would like to hear what you think!

---

<div class="post-metadata">

**Author:** ![atom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/atom/32/113_2.png) [@atom](https://forum.juce.com/u/atom)\
**Post date:** [November 3, 2009, 7:52pm UTC](https://forum.juce.com/t/codeeditorcomponent/4466/2 "2009-11-03T19:52:57Z")

</div>

```auto
atom@suonko:~/devel/juce$ git pull
Already up-to-date.
atom@suonko:~/devel/juce$ grep -R CodeEditorComponent *
atom@suonko:~/devel/juce$
```

what’s the name of the component ?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 3, 2009, 9:03pm UTC](https://forum.juce.com/t/codeeditorcomponent/4466/3 "2009-11-03T21:03:29Z")

</div>

Sorry, I checked it in but forgot to do a ‘git push’… It’s there now!

---

<div class="post-metadata">

**Author:** ![atom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/atom/32/113_2.png) [@atom](https://forum.juce.com/u/atom)\
**Post date:** [November 3, 2009, 9:58pm UTC](https://forum.juce.com/t/codeeditorcomponent/4466/4 "2009-11-03T21:58:50Z")

</div>

it works !

very nice, i didn’t do any extensive tests for now, should we expect any increased performance then the TextEditor ?

Will it handle NULLs in code ?

the tokenizer class looks complicated at first glance. the CPP tokeniser looks impressive, and a good codebase for other tokenisers i think.

great stuff.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 3, 2009, 10:13pm UTC](https://forum.juce.com/t/codeeditorcomponent/4466/5 "2009-11-03T22:13:06Z")

</div>

Yes, it’ll be vastly faster than the texteditor. I tried using it to open the mighty juce\_amalgamated.cpp and it handled it just fine.

[quote]  
Will it handle NULLs in code ?[/quote]  
no chance!

---

<div class="post-metadata">

**Author:** ![atom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/atom/32/113_2.png) [@atom](https://forum.juce.com/u/atom)\
**Post date:** [November 4, 2009, 10:32am UTC](https://forum.juce.com/t/codeeditorcomponent/4466/6 "2009-11-04T10:32:15Z")

</div>

Just an idea.

Do you think it would be more flexible to make the tokenizer class read some sort of a serialized file, that would hold all keywords, functions and reserved words, instead of hard-coding it in the class.

This is what SCITE does, it has special .api files that you can load on demand, and the code highlighting changes. It’s opensource, and you can have a look at some examples here: [http://groups.google.com/group/scite-interest/web/extras?pli=1](http://groups.google.com/group/scite-interest/web/extras?pli=1)

i guess it would be better if those files where XML files with a strict schema described for those tokeniser classes.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 4, 2009, 10:52am UTC](https://forum.juce.com/t/codeeditorcomponent/4466/7 "2009-11-04T10:52:06Z")

</div>

Well, the tokeniser base class could be used to write a parser for those scite files, no problem. But I just wanted to get something going, which is why I wrote the c++ one.

It’d have to be very well optimised though - the tokeniser is the main thing that eats cpu in the editor, and even with my simple c++ parser I had to hand-optimise bits of it to make it run nicely.

---

<div class="post-metadata">

**Author:** ![atom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/atom/32/113_2.png) [@atom](https://forum.juce.com/u/atom)\
**Post date:** [November 4, 2009, 1:06pm UTC](https://forum.juce.com/t/codeeditorcomponent/4466/8 "2009-11-04T13:06:58Z")

</div>

Since i wanted to use the code editor for plaintext files, i just added this code, it would be a nice fallback class

```auto
class PlainTextTokeniser : public CodeTokeniser
{
	public:
		int readNextToken (CodeDocument::Iterator& source) 
		{
			source.skip();
			return (0);
		}
		
		const StringArray getTokenTypes()
		{
			StringArray s;
			s.add (String::empty);
			return (s);
		}

		const Colour getDefaultColour (const int tokenType) 
		{ 
			return (Colours::black); 
		}
};
```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 4, 2009, 1:12pm UTC](https://forum.juce.com/t/codeeditorcomponent/4466/9 "2009-11-04T13:12:10Z")

</div>

You can actually pass 0 for the tokeniser to do plain-text.

---

<div class="post-metadata">

**Author:** ![vishvesh](https://avatars.discourse-cdn.com/v4/letter/v/85e7bf/32.png) [@vishvesh](https://forum.juce.com/u/vishvesh)\
**Post date:** [November 4, 2009, 1:16pm UTC](https://forum.juce.com/t/codeeditorcomponent/4466/10 "2009-11-04T13:16:02Z")

</div>

Does that means that I can use this component instead of Text Editor(since efficiency is better)

---

<div class="post-metadata">

**Author:** ![atom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/atom/32/113_2.png) [@atom](https://forum.juce.com/u/atom)\
**Post date:** [November 4, 2009, 1:21pm UTC](https://forum.juce.com/t/codeeditorcomponent/4466/11 "2009-11-04T13:21:04Z")

</div>

well i didn’t know that :), i’ll do that

also since i have a find feature in my editor, there was a nice handy method for selecting a portion of text in the TextEditor, i can’t find that in the new component i have to do

```auto
CodeDocument::Position pos(dataSource, matchIndex);
editorComponent->moveCaretTo (pos, false);
pos.setPosition (matchIndex + match.length());
editorComponent->moveCaretTo (pos, true);
```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 4, 2009, 1:28pm UTC](https://forum.juce.com/t/codeeditorcomponent/4466/12 "2009-11-04T13:28:53Z")

</div>

Just checked in a version with better comments and a few tweaks.

I’ll be adding a lot more useful methods for finding and selection as it progresses…

[quote]  
Does that means that I can use this component instead of Text Editor(since efficiency is better)[/quote]

Of course - if you’re ok with fixed-width fonts and no line wrapping.

---

<div class="post-metadata">

**Author:** ![vishvesh](https://avatars.discourse-cdn.com/v4/letter/v/85e7bf/32.png) [@vishvesh](https://forum.juce.com/u/vishvesh)\
**Post date:** [November 4, 2009, 3:30pm UTC](https://forum.juce.com/t/codeeditorcomponent/4466/13 "2009-11-04T15:30:55Z")

</div>

[quote=“jules”]Just checked in a version with better comments and a few tweaks.

I’ll be adding a lot more useful methods for finding and selection as it progresses…

[quote]  
Does that means that I can use this component instead of Text Editor(since efficiency is better)[/quote]

Of course - if you’re ok with fixed-width fonts and no line wrapping.[/quote]

In that case, it can’t replace text editor everywhere but there are places where I can find it useful. Thanks Jules

---

<div class="post-metadata">

**Author:** ![red\_muze](https://avatars.discourse-cdn.com/v4/letter/r/91b2a8/32.png) [@red\_muze](https://forum.juce.com/u/red_muze)\
**Post date:** [November 7, 2009, 1:32am UTC](https://forum.juce.com/t/codeeditorcomponent/4466/14 "2009-11-07T01:32:55Z")

</div>

wow this came just on time!!! amaizing stuff!

i have a request for a function:  
setScrollbarThickness(int);

and  
setScrollbarListener(Listener);

so i can make a line numbers module  
🙂

---

<div class="post-metadata">

**Author:** ![valley](https://avatars.discourse-cdn.com/v4/letter/v/67e7ee/32.png) [@valley](https://forum.juce.com/u/valley)\
**Post date:** [November 7, 2009, 2:09am UTC](https://forum.juce.com/t/codeeditorcomponent/4466/15 "2009-11-07T02:09:27Z")

</div>

> [@jules](#):
>
> I mentioned this a while back and a few people expressed an interest, so feel free to have a look and see what you think of the approach I’ve taken.

Speaking as one of those happy few, I love you man.

I’ll dig into this just as soon as I have time enough to stop and breathe. 🙂

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 8, 2009, 6:51pm UTC](https://forum.juce.com/t/codeeditorcomponent/4466/16 "2009-11-08T18:51:13Z")

</div>

[quote]i have a request for a function:  
setScrollbarThickness(int);

and  
setScrollbarListener(Listener); [/quote]

Probably be better to have a listener that gets told when the cursor moves?

I’m also planning on allowing the gutter to be a custom component, so that might be better for what you’re trying to do.

---

<div class="post-metadata">

**Author:** ![red\_muze](https://avatars.discourse-cdn.com/v4/letter/r/91b2a8/32.png) [@red\_muze](https://forum.juce.com/u/red_muze)\
**Post date:** [November 8, 2009, 11:17pm UTC](https://forum.juce.com/t/codeeditorcomponent/4466/17 "2009-11-08T23:17:53Z")

</div>

the cursor itself? or the top line position?  
yes indeed a custom gutter component or a virtual paintGutter would do,  
i thought about just editing the paint function

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 9, 2009, 12:32pm UTC](https://forum.juce.com/t/codeeditorcomponent/4466/18 "2009-11-09T12:32:44Z")

</div>

> [@](#):
>
> the cursor itself? or the top line position?

A listener would be able to provide both.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 12, 2017, 9:50am UTC](https://forum.juce.com/t/codeeditorcomponent/4466/19 "2017-05-12T09:50:37Z")

</div>

Closed on May 12, 2017, 9:50am UTC


