Re: [ace] Can addDynamicMarker be used to track a range of text?

1,412 views
Skip to first unread message

Harutyun Amirjanyan

unread,
Oct 5, 2012, 3:35:42 AM10/5/12
to ace-d...@googlegroups.com
Yes.

If you are going to have many ranges which are sorted you can reuse range_list
https://github.com/ajaxorg/ace/blob/master/lib/ace/range_list.js#L178-218

for a few ranges creating anchors can work as well
https://github.com/ajaxorg/ace/blob/master/lib/ace/placeholder.js#L153
https://github.com/ajaxorg/ace/blob/master/lib/ace/anchor.js#L105

or you can simply create your own onchange listener

Ari Hershowitz

unread,
Oct 18, 2012, 6:20:38 PM10/18/12
to ace-d...@googlegroups.com
I've been trying to get this to work, without success.  These objects (RangeList, PlaceHolder and Anchor) have private Onchange functions.  However, addDynamicMarker seems to require an update function.  I tried assigning RangeList.update = RangeList.$onChange, but this is not working.

What I am looking to do is to select a range of text, assign it a style (e.g. yellow background) and have that text maintain its style even when a user types text before or inside the assigned range.

Any thoughts on how to do this?

Ari

Harutyun Amirjanyan

unread,
Oct 19, 2012, 3:39:00 AM10/19/12
to ace-d...@googlegroups.com
hmm, i see names here are a bit confusing
update should be called draw instead, it provides html to render markers.

the simplest way to do what you are looking for is

//get selection range
var r = editor.getSelectionRange()

//add marker
var session = editor.session
r.start = session.doc.createAnchor(r.start)
r.end = session.doc.createAnchor(r.end)
r.id = session.addMarker(r, "ace_step", "text")

//to remove the marker
r.end.detach()
r.start.detach()
session.removeMarker(r.id)

this should be fast enough for up to several hundred markers

Ari Hershowitz

unread,
Oct 19, 2012, 1:20:45 PM10/19/12
to ace-d...@googlegroups.com
Thank you!  I now have this working.

When I build out the functionality around this, I will probably come back to you to ask about what to do for performance when more markers are added...

Your quick and accurate reply confirms that Ace has the best support of any open source project I've seen!

Ari

Louis-Dominique Dubeau

unread,
Feb 28, 2013, 3:38:36 PM2/28/13
to ace-d...@googlegroups.com


On Tuesday, February 5, 2013 1:40:44 PM UTC-5, Rodrigo de Oliveira wrote:
I don't know if I understood this correctly, but by creating anchors, is the marker supposed to stay binded to the lines? I mean, if the user inserts or deletes a line above the range, the marker follows the range original lines around. 

If the answer is yes, it's not working for me. The range stays binded to the line numbers and doesn't scoot down/up when a line is inserted/deleted. 

The answer is yes, but I ran into the same problem you describe. One thing which is not evident is that you must set the start and end members of the Range directly from the anchors you create. So something like this

 var start = session.doc.createAnchor(a, b);
 var end = session.doc.createAnchor(c, d);
 var range = new Range(start.row, start.column, end.row, end.column);

won't work. You must do something like this:

 var r = new Range(); // Creates a range with undefined values.
 r.start = session.doc.createAnchor(a, b);
 r.end = session.doc.createAnchor(c, d);


Reply all
Reply to author
Forward
0 new messages