From: khartlage Date: Fri, 19 Sep 2003 20:22:35 +0000 (+0000) Subject: bug fix: HTML Context information should be show properlyfor completion processor X-Git-Url: http://secure.phpeclipse.com bug fix: HTML Context information should be show properlyfor completion processor --- diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/TemplateProposal.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/TemplateProposal.java index cf5c204..0b9e8b7 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/TemplateProposal.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/TemplateProposal.java @@ -35,208 +35,208 @@ import org.eclipse.swt.widgets.Shell; */ public class TemplateProposal implements IPHPCompletionProposal { - private final Template fTemplate; - private final TemplateContext fContext; - private final ITextViewer fViewer; - private final Image fImage; - private final IRegion fRegion; - - private TemplateBuffer fTemplateBuffer; - private String fOldText; - private IRegion fSelectedRegion; // initialized by apply() - - /** - * Creates a template proposal with a template and its context. - * @param template the template - * @param context the context in which the template was requested. - * @param image the icon of the proposal. - */ - public TemplateProposal(Template template, TemplateContext context, IRegion region, ITextViewer viewer, Image image) { -// Assert.isNotNull(template); -// Assert.isNotNull(context); -// Assert.isNotNull(region); -// Assert.isNotNull(viewer); - - fTemplate= template; - fContext= context; - fViewer= viewer; - fImage= image; - fRegion= region; - } - - /* - * @see ICompletionProposal#apply(IDocument) - */ - public void apply(IDocument document) { - try { - if (fTemplateBuffer == null) - fTemplateBuffer= fContext.evaluate(fTemplate); - - int start= fRegion.getOffset(); - int end= fRegion.getOffset() + fRegion.getLength(); - - // insert template string - String templateString= fTemplateBuffer.getString(); - document.replace(start, end - start, templateString); - - // translate positions - LinkedPositionManager manager= new LinkedPositionManager(document); - TemplatePosition[] variables= fTemplateBuffer.getVariables(); - for (int i= 0; i != variables.length; i++) { - TemplatePosition variable= variables[i]; - - if (variable.isResolved()) - continue; - - int[] offsets= variable.getOffsets(); - int length= variable.getLength(); - - for (int j= 0; j != offsets.length; j++) - manager.addPosition(offsets[j] + start, length); - } - - LinkedPositionUI editor= new LinkedPositionUI(fViewer, manager); - editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start); - editor.enter(); - - fSelectedRegion= editor.getSelectedRegion(); - - } catch (BadLocationException e) { - PHPeclipsePlugin.log(e); - openErrorDialog(e); - - } catch (CoreException e) { - handleException(e); - } - } - - private static int getCaretOffset(TemplateBuffer buffer) { - TemplatePosition[] variables= buffer.getVariables(); - for (int i= 0; i != variables.length; i++) { - TemplatePosition variable= variables[i]; - - if (variable.getName().equals(PHPTemplateMessages.getString("GlobalVariables.variable.name.cursor"))) //$NON-NLS-1$ - return variable.getOffsets()[0]; - } - - return buffer.getString().length(); - } - - /* - * @see ICompletionProposal#getSelection(IDocument) - */ - public Point getSelection(IDocument document) { - return new Point(fSelectedRegion.getOffset(), fSelectedRegion.getLength()); -// return null; + private final Template fTemplate; + private final TemplateContext fContext; + private final ITextViewer fViewer; + private final Image fImage; + private final IRegion fRegion; + + private TemplateBuffer fTemplateBuffer; + private String fOldText; + private IRegion fSelectedRegion; // initialized by apply() + + /** + * Creates a template proposal with a template and its context. + * @param template the template + * @param context the context in which the template was requested. + * @param image the icon of the proposal. + */ + public TemplateProposal(Template template, TemplateContext context, IRegion region, ITextViewer viewer, Image image) { + // Assert.isNotNull(template); + // Assert.isNotNull(context); + // Assert.isNotNull(region); + // Assert.isNotNull(viewer); + + fTemplate = template; + fContext = context; + fViewer = viewer; + fImage = image; + fRegion = region; } - /* - * @see ICompletionProposal#getAdditionalProposalInfo() - */ - public String getAdditionalProposalInfo() { - try { - if (fTemplateBuffer == null) - fTemplateBuffer= fContext.evaluate(fTemplate); - - return textToHTML(fTemplateBuffer.getString()); - - } catch (CoreException e) { - handleException(e); - return null; - } - } - - /* - * @see ICompletionProposal#getDisplayString() - */ - public String getDisplayString() { - return fTemplate.getName() + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$ - } - - /* - * @see ICompletionProposal#getImage() - */ - public Image getImage() { - return fImage; - } - - /* - * @see ICompletionProposal#getContextInformation() - */ - public IContextInformation getContextInformation() { - return null; - } - - private static String textToHTML(String string) { - StringBuffer buffer= new StringBuffer(string.length()); - buffer.append("
"); //$NON-NLS-1$
-	
-		for (int i= 0; i != string.length(); i++) {
-			char ch= string.charAt(i);
-			
-			switch (ch) {
-				case '&':
-					buffer.append("&"); //$NON-NLS-1$
-					break;
-					
-				case '<':
-					buffer.append("<"); //$NON-NLS-1$
-					break;
-
-				case '>':
-					buffer.append(">"); //$NON-NLS-1$
-					break;
-
-				case '\t':
-					buffer.append("    "); //$NON-NLS-1$
-					break;
-
-				case '\n':
-					buffer.append("
"); //$NON-NLS-1$ - break; - - default: - buffer.append(ch); - break; - } - } - - buffer.append("
"); //$NON-NLS-1$ - return buffer.toString(); - } - - private void openErrorDialog(BadLocationException e) { - Shell shell= fViewer.getTextWidget().getShell(); - MessageDialog.openError(shell, TemplateMessages.getString("TemplateEvaluator.error.title"), e.getMessage()); //$NON-NLS-1$ - } - - private void handleException(CoreException e) { - Shell shell= fViewer.getTextWidget().getShell(); + /* + * @see ICompletionProposal#apply(IDocument) + */ + public void apply(IDocument document) { + try { + if (fTemplateBuffer == null) + fTemplateBuffer = fContext.evaluate(fTemplate); + + int start = fRegion.getOffset(); + int end = fRegion.getOffset() + fRegion.getLength(); + + // insert template string + String templateString = fTemplateBuffer.getString(); + document.replace(start, end - start, templateString); + + // translate positions + LinkedPositionManager manager = new LinkedPositionManager(document); + TemplatePosition[] variables = fTemplateBuffer.getVariables(); + for (int i = 0; i != variables.length; i++) { + TemplatePosition variable = variables[i]; + + if (variable.isResolved()) + continue; + + int[] offsets = variable.getOffsets(); + int length = variable.getLength(); + + for (int j = 0; j != offsets.length; j++) + manager.addPosition(offsets[j] + start, length); + } + + LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager); + editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start); + editor.enter(); + + fSelectedRegion = editor.getSelectedRegion(); + + } catch (BadLocationException e) { + PHPeclipsePlugin.log(e); + openErrorDialog(e); + + } catch (CoreException e) { + handleException(e); + } + } + + private static int getCaretOffset(TemplateBuffer buffer) { + TemplatePosition[] variables = buffer.getVariables(); + for (int i = 0; i != variables.length; i++) { + TemplatePosition variable = variables[i]; + + if (variable.getName().equals(PHPTemplateMessages.getString("GlobalVariables.variable.name.cursor"))) //$NON-NLS-1$ + return variable.getOffsets()[0]; + } + + return buffer.getString().length(); + } + + /* + * @see ICompletionProposal#getSelection(IDocument) + */ + public Point getSelection(IDocument document) { + return new Point(fSelectedRegion.getOffset(), fSelectedRegion.getLength()); + // return null; + } + + /* + * @see ICompletionProposal#getAdditionalProposalInfo() + */ + public String getAdditionalProposalInfo() { + try { + if (fTemplateBuffer == null) + fTemplateBuffer = fContext.evaluate(fTemplate); + + return textToHTML(fTemplateBuffer.getString()); + + } catch (CoreException e) { + handleException(e); + return null; + } + } + + /* + * @see ICompletionProposal#getDisplayString() + */ + public String getDisplayString() { + return fTemplate.getName() + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$ + } + + /* + * @see ICompletionProposal#getImage() + */ + public Image getImage() { + return fImage; + } + + /* + * @see ICompletionProposal#getContextInformation() + */ + public IContextInformation getContextInformation() { + return null; + } + + private static String textToHTML(String string) { + StringBuffer buffer = new StringBuffer(string.length()); + buffer.append("
"); //$NON-NLS-1$
+
+    for (int i = 0; i != string.length(); i++) {
+      char ch = string.charAt(i);
+
+      switch (ch) {
+        case '&' :
+          buffer.append("&"); //$NON-NLS-1$
+          break;
+
+        case '<' :
+          buffer.append("<"); //$NON-NLS-1$
+          break;
+
+        case '>' :
+          buffer.append(">"); //$NON-NLS-1$
+          break;
+
+        case '\t' :
+          buffer.append("    "); //$NON-NLS-1$
+          break;
+
+        case '\n' :
+          buffer.append("
"); //$NON-NLS-1$ + break; + + default : + buffer.append(ch); + break; + } + } + + buffer.append("
"); //$NON-NLS-1$ + return buffer.toString(); + } + + private void openErrorDialog(BadLocationException e) { + Shell shell = fViewer.getTextWidget().getShell(); + MessageDialog.openError(shell, TemplateMessages.getString("TemplateEvaluator.error.title"), e.getMessage()); //$NON-NLS-1$ + } + + private void handleException(CoreException e) { + Shell shell = fViewer.getTextWidget().getShell(); PHPeclipsePlugin.log(e); -// ExceptionHandler.handle(e, shell, ObfuscatorMessages.getString("TemplateEvaluator.error.title"), null); //$NON-NLS-1$ - } - - /* - * @see IJavaCompletionProposal#getRelevance() - */ - public int getRelevance() { - - if (fContext instanceof PHPUnitContext) { - PHPUnitContext context= (PHPUnitContext) fContext; - switch (context.getCharacterBeforeStart()) { - // high relevance after whitespace - case ' ': - case '\r': - case '\n': - case '\t': - return 90; - - default: - return 0; - } - } else { - return 90; - } - } + // ExceptionHandler.handle(e, shell, ObfuscatorMessages.getString("TemplateEvaluator.error.title"), null); //$NON-NLS-1$ + } + + /* + * @see IJavaCompletionProposal#getRelevance() + */ + public int getRelevance() { + + if (fContext instanceof PHPUnitContext) { + PHPUnitContext context = (PHPUnitContext) fContext; + switch (context.getCharacterBeforeStart()) { + // high relevance after whitespace + case ' ' : + case '\r' : + case '\n' : + case '\t' : + return 90; + + default : + return 0; + } + } else { + return 90; + } + } } \ No newline at end of file diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPSourceViewerConfiguration.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPSourceViewerConfiguration.java index 1156dee..ab128ff 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPSourceViewerConfiguration.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPSourceViewerConfiguration.java @@ -14,6 +14,7 @@ package net.sourceforge.phpeclipse.phpeditor; import java.util.Vector; import net.sourceforge.phpdt.internal.ui.text.ContentAssistPreference; +import net.sourceforge.phpdt.internal.ui.text.HTMLTextPresenter; import net.sourceforge.phpdt.internal.ui.text.java.JavaFormattingStrategy; import net.sourceforge.phpdt.internal.ui.text.phpdoc.PHPDocCompletionProcessor; import net.sourceforge.phpdt.ui.PreferenceConstants; @@ -31,8 +32,11 @@ import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider; import org.eclipse.core.resources.IFile; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.text.DefaultAutoIndentStrategy; +import org.eclipse.jface.text.DefaultInformationControl; import org.eclipse.jface.text.IAutoIndentStrategy; import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IInformationControl; +import org.eclipse.jface.text.IInformationControlCreator; import org.eclipse.jface.text.ITextDoubleClickStrategy; import org.eclipse.jface.text.ITextHover; import org.eclipse.jface.text.TextAttribute; @@ -52,6 +56,8 @@ import org.eclipse.jface.text.rules.Token; import org.eclipse.jface.text.source.IAnnotationHover; import org.eclipse.jface.text.source.ISourceViewer; import org.eclipse.jface.text.source.SourceViewerConfiguration; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IFileEditorInput; /** @@ -379,4 +385,17 @@ public class PHPSourceViewerConfiguration extends SourceViewerConfiguration { } return new PHPTextHover(null); } + + /* + * @see SourceViewerConfiguration#getInformationControlCreator(ISourceViewer) + * @since 2.0 + */ + public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) { + return new IInformationControlCreator() { + public IInformationControl createInformationControl(Shell parent) { + return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true)); + // return new HoverBrowserControl(parent); + } + }; + } }