|
From: <caw...@us...> - 2007-05-31 13:38:46
|
Revision: 2568
http://svn.sourceforge.net/rubyeclipse/?rev=2568&view=rev
Author: cawilliams
Date: 2007-05-31 06:38:43 -0700 (Thu, 31 May 2007)
Log Message:
-----------
modify to try to resolve the code first and grab a suitable name, then if that fails just grab text and pass it to RI. Also avoid running RI for hovers in non-code regions (comments) or for text that will obviously fail (variables and symbols)
Modified Paths:
--------------
trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/text/ruby/hover/RiDocHoverProvider.java
Modified: trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/text/ruby/hover/RiDocHoverProvider.java
===================================================================
--- trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/text/ruby/hover/RiDocHoverProvider.java 2007-05-31 13:37:17 UTC (rev 2567)
+++ trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/text/ruby/hover/RiDocHoverProvider.java 2007-05-31 13:38:43 UTC (rev 2568)
@@ -8,15 +8,66 @@
import java.util.List;
import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.BadPartitioningException;
+import org.eclipse.jface.text.IDocumentExtension3;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
+import org.rubypeople.rdt.core.ICodeAssist;
+import org.rubypeople.rdt.core.IMethod;
+import org.rubypeople.rdt.core.IRubyElement;
+import org.rubypeople.rdt.core.RubyModelException;
import org.rubypeople.rdt.internal.ui.RubyPlugin;
+import org.rubypeople.rdt.internal.ui.text.IRubyPartitions;
import org.rubypeople.rdt.launching.RubyRuntime;
public class RiDocHoverProvider extends AbstractRubyEditorTextHover {
- @Override
+ /*
+ * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
+ */
public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
+
+ /*
+ * The region should be a word region an not of length 0.
+ * This check is needed because codeSelect(...) also finds
+ * the Ruby element if the offset is behind the word.
+ */
+ if (hoverRegion.getLength() == 0)
+ return null;
+
+ ICodeAssist resolve= getCodeAssist();
+ if (resolve != null) {
+ try {
+ IRubyElement[] result= resolve.codeSelect(hoverRegion.getOffset(), hoverRegion.getLength());
+ if (result != null && result.length > 0) {
+ return getHoverInfo(result);
+ }
+ } catch (RubyModelException x) {
+ return null;
+ }
+ }
+ try {
+ IDocumentExtension3 extension = (IDocumentExtension3)textViewer.getDocument();
+ String contentType = null;
+ try {
+ contentType = extension.getContentType(IRubyPartitions.RUBY_PARTITIONING, hoverRegion.getOffset(), false);
+ } catch (BadPartitioningException e) {
+ // ignore
+ }
+ if (contentType != null && (contentType.equals(IRubyPartitions.RUBY_MULTI_LINE_COMMENT) || contentType.equals(IRubyPartitions.RUBY_SINGLE_LINE_COMMENT))) {
+ return null;
+ }
+ String symbol = textViewer.getDocument().get(hoverRegion.getOffset(), hoverRegion.getLength());
+ if (symbol != null && (symbol.startsWith("@") || symbol.startsWith("$") || symbol.startsWith(":"))) return null; // don't try class/instance/global variables or symbols
+ return getRIResult(symbol);
+ } catch (BadLocationException e) {
+ // ignore
+ }
+ return null;
+ }
+
+
+ private String getRIResult(String symbol) {
File ri = RubyRuntime.getRI();
if (ri == null || !ri.exists() || !ri.isFile()) return null;
@@ -29,8 +80,7 @@
BufferedReader br = null;
try {
- String symbol = textViewer.getDocument().get(hoverRegion.getOffset(), hoverRegion.getLength());
- args.add(symbol);
+ args.add('"' + symbol + '"');
String[] argArray= (String[]) args.toArray(new String[args.size()]);
Process p = Runtime.getRuntime().exec(argArray);
if (p == null) return null;
@@ -50,9 +100,7 @@
// If ambiguous, return nothing
if (buf.indexOf("More than one method matched your request") > -1) return null;
return "" + buf.toString();
- } catch (BadLocationException e) {
- RubyPlugin.log(e);
- } catch (IOException e) {
+ } catch (IOException e) {
RubyPlugin.log(e);
} finally {
if(br != null){
@@ -65,4 +113,26 @@
}
return null;
}
+
+ @Override
+ protected String getHoverInfo(IRubyElement[] rubyElements) {
+ if (rubyElements == null || rubyElements.length == 0) return null;
+ String symbol = getRICompatibleName(rubyElements[0]);
+ if (symbol == null) return null;
+ return getRIResult(symbol);
+ }
+
+ private String getRICompatibleName(IRubyElement element) {
+ switch (element.getElementType()) {
+ case IRubyElement.TYPE:
+ return element.getElementName();
+ case IRubyElement.METHOD:
+ IMethod method = (IMethod) element;
+ String delimeter = method.isSingleton() ? "::" : "#";
+ return method.getDeclaringType().getElementName() + delimeter + element.getElementName();
+
+ default:
+ return null;
+ }
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|