Bug 520607 -  Hover on command in script freezes UI

This change prevents hanging when loading a large man page in the
hoverhelp by truncating very large pages and instead advising users
to use the show man page command to view the man page in the
dedicated man page view

Change-Id: Ia668cdd01aa235e74ed5e8558bd316c7590c386d
Signed-off-by: Mat Booth <mat.booth@redhat.com>
diff --git a/plugins/org.eclipse.dltk.sh.ui/plugin.xml b/plugins/org.eclipse.dltk.sh.ui/plugin.xml
index b0f844a..26ff717 100644
--- a/plugins/org.eclipse.dltk.sh.ui/plugin.xml
+++ b/plugins/org.eclipse.dltk.sh.ui/plugin.xml
@@ -200,9 +200,9 @@
 		</command>
 		<command
         defaultHandler="org.eclipse.dltk.sh.internal.ui.commands.ShowManHandler"
-        description="Show man page view"
+        description="Show command in the man page view"
         id="org.eclipse.dltk.sh.ui.showman"
-        name="Show man page">
+        name="Show Man Page">
 		</command>
   <command
         defaultHandler="org.eclipse.dltk.sh.internal.ui.commands.AddNature"
@@ -286,6 +286,14 @@
         </visibleWhen>
      </command>
    </menuContribution>
+   <menuContribution
+         allPopups="true"
+         locationURI="popup:org.eclipse.dltk.sh.ui.editor.EditorContext?after=additions">
+      <command
+            commandId="org.eclipse.dltk.sh.ui.showman"
+            style="push">
+      </command>
+   </menuContribution>
 </extension>
  <extension
    	point="org.eclipse.ui.workbench.texteditor.hyperlinkDetectors">
diff --git a/plugins/org.eclipse.dltk.sh.ui/src/org/eclipse/dltk/sh/internal/ui/selection/ShellDocumentationProvider.java b/plugins/org.eclipse.dltk.sh.ui/src/org/eclipse/dltk/sh/internal/ui/selection/ShellDocumentationProvider.java
index 239300e..8787cdb 100644
--- a/plugins/org.eclipse.dltk.sh.ui/src/org/eclipse/dltk/sh/internal/ui/selection/ShellDocumentationProvider.java
+++ b/plugins/org.eclipse.dltk.sh.ui/src/org/eclipse/dltk/sh/internal/ui/selection/ShellDocumentationProvider.java
@@ -30,11 +30,25 @@
 		if (!helpInfo.isEmpty()) {
 			return new StringReader("<pre>" + helpInfo + "</pre>");
 		}
-		String info = new ManPage(content).getStrippedHtmlPage().toString();
-		if (info.equals("<pre></pre>")) {
+		StringBuilder manInfo = new ManPage(content).getStrippedPage();
+		if (manInfo.length() == 0) {
 			return null;
 		}
-		return new StringReader(info);
+		// Any more than ~10kb and the hover help starts to take a really long time to
+		// render, so truncate it in that case and advise to use the man page view
+		final int maxLen = 1024 * 10;
+		StringBuilder sb = new StringBuilder("<pre>");
+		if (manInfo.length() > maxLen) {
+			sb.append(manInfo.substring(0, maxLen));
+		} else {
+			sb.append(manInfo);
+		}
+		sb.append("</pre>");
+		if (manInfo.length() > maxLen) {
+			sb.append(
+					"<p>This man page is truncated, use <b>Show Man Page</b> (Alt+M) to see the whole page for the selected word.</p>");
+		}
+		return new StringReader(sb.toString());
 	}
 
 	private static String getHelp(String command) {