Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "FAQ How do I add hover support to my text editor?"

 
Line 1: Line 1:
 +
== If you're using the Generic and Extensible editor ==
 +
 +
Add hover support via the <tt>org.eclipse.ui.genericeditor.hoverProviders</tt> extension point, providing an implemention <tt>ITextHover</tt>.
 +
 +
== If you're extensing the StructuredTextEditor ==
 +
 +
TODO
 +
 +
== If it's your own editor ==
 
In [[FAQ How do I write an editor for my own language?]] we describe how  text hover is enabled for our editor through our configuration class:
 
In [[FAQ How do I write an editor for my own language?]] we describe how  text hover is enabled for our editor through our configuration class:
<pre>
+
<source lang="java">
 
   class Configuration extends SourceViewerConfiguration {
 
   class Configuration extends SourceViewerConfiguration {
 
       ...
 
       ...
 
       public ITextHover getTextHover(ISourceViewer sv,  
 
       public ITextHover getTextHover(ISourceViewer sv,  
 
       String contentType) {
 
       String contentType) {
         return '''new TextHover()''';
+
         return '''new EScriptTextHover()''';
 
       }
 
       }
 
       ...
 
       ...
 
   }
 
   }
</pre>
+
</source>
 +
 
 +
== Example of ITextHover implementation ==
 +
 
 
When the user moves the mouse over an area that corresponds to a given node in our AST, it is easy for us to provide a symbolic description of the node. Namely, the editor framework helps out by registering for the mouse events, setting timers, calling us at the right time, and  drawing the box that will show the text hover. All that we need to do is match a certain location in the editor to a symbolic string.We do this by providing our own implementation of <tt>org.eclipse.jface.text.ITextHover</tt> as follows:
 
When the user moves the mouse over an area that corresponds to a given node in our AST, it is easy for us to provide a symbolic description of the node. Namely, the editor framework helps out by registering for the mouse events, setting timers, calling us at the right time, and  drawing the box that will show the text hover. All that we need to do is match a certain location in the editor to a symbolic string.We do this by providing our own implementation of <tt>org.eclipse.jface.text.ITextHover</tt> as follows:
<pre>
+
<source lang="java">
   public class TextHover implements ITextHover {
+
   public class EScriptTextHover implements ITextHover {
 
       public IRegion getHoverRegion(ITextViewer tv, int off) {
 
       public IRegion getHoverRegion(ITextViewer tv, int off) {
 
         return new Region(off, 0);
 
         return new Region(off, 0);
Line 28: Line 40:
 
       }
 
       }
 
   }
 
   }
</pre>
+
</source>
 
The first method we implement is meant for optimizing the drawing of the text hover. We  answer the question, If I am going to show a hover for character x in the text viewer, for what region should the hover be the same? We don&#146;t try to be too smart here. We simply return an empty region.
 
The first method we implement is meant for optimizing the drawing of the text hover. We  answer the question, If I am going to show a hover for character x in the text viewer, for what region should the hover be the same? We don&#146;t try to be too smart here. We simply return an empty region.
  

Latest revision as of 05:37, 8 November 2016

If you're using the Generic and Extensible editor

Add hover support via the org.eclipse.ui.genericeditor.hoverProviders extension point, providing an implemention ITextHover.

If you're extensing the StructuredTextEditor

TODO

If it's your own editor

In FAQ How do I write an editor for my own language? we describe how text hover is enabled for our editor through our configuration class:

   class Configuration extends SourceViewerConfiguration {
      ...
      public ITextHover getTextHover(ISourceViewer sv, 
       String contentType) {
         return '''new EScriptTextHover()''';
      }
      ...
   }

Example of ITextHover implementation

When the user moves the mouse over an area that corresponds to a given node in our AST, it is easy for us to provide a symbolic description of the node. Namely, the editor framework helps out by registering for the mouse events, setting timers, calling us at the right time, and drawing the box that will show the text hover. All that we need to do is match a certain location in the editor to a symbolic string.We do this by providing our own implementation of org.eclipse.jface.text.ITextHover as follows:

   public class EScriptTextHover implements ITextHover {
      public IRegion getHoverRegion(ITextViewer tv, int off) {
         return new Region(off, 0);
      }
      public String getHoverInfo(ITextViewer tv, IRegion r) {
         try {
            IDocument doc = tv.getDocument();
            EscriptModel em = EscriptModel.getModel(doc, null);
            return em.getElementAt(r.getOffset()).
               getHoverHelp();
         }
         catch (Exception e) {            
            return ""; 
         }
      }
   }

The first method we implement is meant for optimizing the drawing of the text hover. We answer the question, If I am going to show a hover for character x in the text viewer, for what region should the hover be the same? We don&#146;t try to be too smart here. We simply return an empty region.

The next method implements the real logic of the text hover. We convert the current cursor location to an AST element in the document and ask it to return a string relevant to the current context. Note that we assume that the EscriptModel implements a cache and that the getModel method is inexpensive as we will call it many times during editing.


See Also:

FAQ How do I create problem markers for my compiler?


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.

Back to the top