Skip to main content



      Home
Home » Newcomers » Newcomers » Find the current caret position in a TextEditor
Find the current caret position in a TextEditor [message #242501] Fri, 07 December 2007 17:15 Go to next message
Eclipse UserFriend
I'm writing an extension plug-in to any text editor.
The plug-in gets events when the text is selected and it is easy to
replace the selected text with the required text.
However, I want to have the ability to insert text into the current
caret position.
How can I find the current caret position in the text editor?

From the start event I have the ITextEditor and I can get the IDocument
as well. But I have no access to the getCaretPosition method. Since
there is no selection highlighted, the selection object is empty.

Thanks
Yigal
Re: Find the current caret position in a TextEditor [message #242516 is a reply to message #242501] Fri, 07 December 2007 17:30 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: claudiofc2.yahoo.com.ar

I don't know how to do it if all you have is an ITextEditor, but in the
editor I'm working on, which extends
org.eclipse.ui.editors.text.TextEditor, I can do it using the
ISourceViewer, then a possible code inside my editor could be:

Position position = this.getSourceViewer().getSelectedRange();

And to replace something, I can do something like:

idocument.replace(point.x, 0, "</" + str + ">");

Regards and I hope it helps!!
Claudio
Re: Find the current caret position in a TextEditor [message #242559 is a reply to message #242516] Sat, 08 December 2007 08:42 Go to previous messageGo to next message
Eclipse UserFriend
Claudio Cancinos wrote:
> I don't know how to do it if all you have is an ITextEditor, but in the
> editor I'm working on, which extends
> org.eclipse.ui.editors.text.TextEditor, I can do it using the
> ISourceViewer, then a possible code inside my editor could be:
>
> Position position = this.getSourceViewer().getSelectedRange();
>
> And to replace something, I can do something like:
>
> idocument.replace(point.x, 0, "</" + str + ">");
>
> Regards and I hope it helps!!
> Claudio
>
I need to give you more information. I've created a plugin that extended
the org.eclipse.ui.popupMenu. It adds a new menu item on any popup
menu in any text editor.
The main action class in the plugin receives a notification with the
current editor. The getSourceViewer is protected and not accesseable.
My code need to insert text at current caret position. How can I find
the current position from a TextEditor when all methods are protected?

Yigal.
Re: Find the current caret position in a TextEditor [message #242614 is a reply to message #242559] Sun, 09 December 2007 08:09 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: ronbermejo.nospam.abc

Hi!

Please try:

StyledText st = (StyledText) editor.getAdapter(Control.class);
st.getCaretOffset()

Hth!
Ron
Re: Find the current caret position in a TextEditor [message #242796 is a reply to message #242614] Tue, 11 December 2007 06:30 Go to previous message
Eclipse UserFriend
Ron Bermejo wrote:
> Hi!
>
> Please try:
>
> StyledText st = (StyledText) editor.getAdapter(Control.class);
> st.getCaretOffset()
>
> Hth!
> Ron
>
Thanks Ron - it works very nicly!!!
Previous Topic:Configuring selection of text with double click
Next Topic:Removing LazyLoading of TreeViewer
Goto Forum:
  


Current Time: Fri Jul 18 06:12:44 EDT 2025

Powered by FUDForum. Page generated in 0.11065 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top