Commit ad22fc82 authored by Miklos Magyari's avatar Miklos Magyari
Browse files

Initial implementation of Ttcn3HoverInfoControl


Signed-off-by: Miklos Magyari's avatarMiklos Magyari <miklos.magyari@sigmatechnology.se>
parent 403caf5f
/******************************************************************************
* Copyright (c) 2000-2021 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
******************************************************************************/
package org.eclipse.titan.designer.editors.controls;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.custom.StyleRange;
/**
* This class represents a styled text
* Used for ttcn3 editor hover support returned by getHoverInfo2 as an 'object'
*
* @author Miklos Magyari
*/
public class Ttcn3HoverContent {
private String text;
private List<StyleRange> ranges = new ArrayList<StyleRange>();
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public List<StyleRange> getRanges() {
return ranges;
}
public void addRange(StyleRange range) {
ranges.add(range);
}
public void addRangeAll(List<StyleRange> ranges) {
this.ranges.addAll(ranges);
}
}
/******************************************************************************
* Copyright (c) 2000-2021 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
******************************************************************************/
package org.eclipse.titan.designer.editors.controls;
import org.eclipse.jface.text.AbstractInformationControl;
import org.eclipse.jface.text.IInformationControlExtension2;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
/**
* Control for handling ttcn3 source editor hovers
*
* @author Miklos Magyari
*
*/
public class Ttcn3HoverInfoControl extends AbstractInformationControl implements IInformationControlExtension2 {
private StyledText viewer;
private Ttcn3HoverContent content;
private Shell fShell;
public Ttcn3HoverInfoControl(Shell parentShell, String string) {
super(parentShell, string);
createContent(parentShell);
create();
}
@Override
@Deprecated
public void setInformation(String information) {
// setInput is used instead
}
@Override
public void setFocus() {
super.setFocus();
if (fShell != null)
fShell.setFocus();
}
@Override
public boolean hasContents() {
return content.getText() != null;
}
@Override
protected void createContent(Composite parent) {
fShell = parent.getShell();
viewer = new StyledText(parent, 0);
viewer.setEditable(false);
viewer.setBackground(fShell.getBackground());
setStatusText("Hover or press F2 to focus");
}
@Override
public void setInput(Object input) {
if (input instanceof Ttcn3HoverContent) {
content = (Ttcn3HoverContent)input;
viewer.setText(content.getText());
for (StyleRange style : content.getRanges()) {
viewer.setStyleRange(style);
}
}
}
}
......@@ -48,7 +48,7 @@ import org.eclipse.ui.PlatformUI;
public class Ttcn3SourceViewControl implements IInformationControl, IInformationControlExtension2 {
private static Ttcn3SourceViewControl instance = null;
private static Shell myshell;
private static Shell fShell;
private static StyledText viewer;
private int maxWidth = SWT.DEFAULT;
......@@ -67,11 +67,11 @@ public class Ttcn3SourceViewControl implements IInformationControl, IInformation
}
public void createControl(Shell parent) {
if (myshell != null) {
if (fShell != null) {
return;
}
myshell = new Shell(parent, SWT.TOOL | SWT.LEFT_TO_RIGHT | SWT.RESIZE);
Composite composite = myshell;
fShell = new Shell(parent, SWT.TOOL | SWT.LEFT_TO_RIGHT | SWT.RESIZE);
Composite composite = fShell;
GridLayout layout = new GridLayout(1, false);
layout.marginHeight = 10;
layout.marginWidth = 10;
......@@ -89,7 +89,7 @@ public class Ttcn3SourceViewControl implements IInformationControl, IInformation
/**
* event listener for ESC
*/
myshell.addListener(SWT.Traverse, new Listener() {
fShell.addListener(SWT.Traverse, new Listener() {
public void handleEvent(Event event) {
switch (event.detail) {
case SWT.TRAVERSE_ESCAPE:
......@@ -198,47 +198,47 @@ public class Ttcn3SourceViewControl implements IInformationControl, IInformation
@Override
public Point computeSizeHint() {
myshell.layout();
return myshell.computeSize(SWT.DEFAULT, SWT.DEFAULT);
fShell.layout();
return fShell.computeSize(SWT.DEFAULT, SWT.DEFAULT);
}
@Override
public void setVisible(boolean visible) {
myshell.setVisible(visible);
fShell.setVisible(visible);
}
@Override
public void setSize(int width, int height) {
myshell.layout();
Rectangle displayRect = myshell.getDisplay().getClientArea();
Rectangle shellRect = myshell.getBounds();
fShell.layout();
Rectangle displayRect = fShell.getDisplay().getClientArea();
Rectangle shellRect = fShell.getBounds();
maxWidth = displayRect.width - shellRect.x;
maxHeight = displayRect.height - shellRect.y;
myshell.setSize(Math.min(width, maxWidth), Math.min(height, maxHeight));
fShell.setSize(Math.min(width, maxWidth), Math.min(height, maxHeight));
}
@Override
public void setLocation(Point location) {
myshell.setLocation(location);
fShell.setLocation(location);
}
@Override
public void setBackgroundColor(Color background) {
myshell.setBackground(background);
fShell.setBackground(background);
}
@Override
public void setFocus() {
myshell.setFocus();
fShell.setFocus();
}
@Override
public void dispose() {
instance = null;
if (myshell != null && ! myshell.isDisposed()) {
myshell.dispose();
if (fShell != null && ! fShell.isDisposed()) {
fShell.dispose();
}
myshell = null;
fShell = null;
}
@Override
......
......@@ -7,18 +7,21 @@
******************************************************************************/
package org.eclipse.titan.designer.editors.ttcn3editor;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.DefaultInformationControl;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IInformationControl;
import org.eclipse.jface.text.IInformationControlCreator;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextHoverExtension;
import org.eclipse.jface.text.ITextHoverExtension2;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.titan.designer.AST.Assignment;
import org.eclipse.titan.designer.AST.DocumentComment;
......@@ -42,6 +45,8 @@ import org.eclipse.titan.designer.AST.TTCN3.types.Class_Type;
import org.eclipse.titan.designer.AST.TTCN3.types.Referenced_Type;
import org.eclipse.titan.designer.editors.BaseTextHover;
import org.eclipse.titan.designer.editors.IReferenceParser;
import org.eclipse.titan.designer.editors.controls.Ttcn3HoverContent;
import org.eclipse.titan.designer.editors.controls.Ttcn3HoverInfoControl;
import org.eclipse.titan.designer.parsers.CompilationTimeStamp;
import org.eclipse.titan.designer.parsers.GlobalParser;
import org.eclipse.titan.designer.parsers.ProjectSourceParser;
......@@ -53,7 +58,7 @@ import org.eclipse.ui.editors.text.EditorsUI;
* @author Kristof Szabados
* @author Miklos Magyari
* */
public final class TextHover extends BaseTextHover implements ITextHoverExtension {
public final class TextHover extends BaseTextHover implements ITextHoverExtension, ITextHoverExtension2 {
private final ISourceViewer sourceViewer;
private final IEditorPart editor;
......@@ -78,14 +83,14 @@ public final class TextHover extends BaseTextHover implements ITextHoverExtensio
}
/**
* Preliminary code for ttcn3 editor hover support
* Ttcn3 editor hover support
*
* @param textViewer
* @param hoverRegion
* @return
*/
@Override
public String getHoverInfo(final ITextViewer textViewer, final IRegion hoverRegion) {
public Object getHoverInfo2(final ITextViewer textViewer, final IRegion hoverRegion) {
IDocument doc = textViewer.getDocument();
final IFile file = editor.getEditorInput().getAdapter(IFile.class);
......@@ -110,10 +115,10 @@ public final class TextHover extends BaseTextHover implements ITextHoverExtensio
Assignment assignment = scope.getAssBySRef(timestamp, ref);
if (assignment instanceof Definition) {
DocumentComment dc = ((Definition) assignment).getDocumentComment();
List<StyleRange> styles = new ArrayList<StyleRange>();
StringBuilder tooltip = new StringBuilder();
tooltip.append("<html>");
if (assignment instanceof Def_Function) {
functionHover(tooltip, dc, assignment);
functionHover(tooltip, styles, dc, assignment);
}
if (assignment instanceof Def_Const) {
constantHover(tooltip, dc, assignment, timestamp);
......@@ -131,13 +136,28 @@ public final class TextHover extends BaseTextHover implements ITextHoverExtensio
default:
}
}
tooltip.append("</html>");
return tooltip.toString();
Ttcn3HoverContent content = new Ttcn3HoverContent();
content.setText(tooltip.toString());
content.addRangeAll(styles);
return content;
}
}
return "";
}
/**
* Legacy inteface method implemented for completeness
*
* getHoverInfo2 is used instead
*/
@Override
public String getHoverInfo(final ITextViewer textViewer, final IRegion hoverRegion) {
final Ttcn3HoverContent content = (Ttcn3HoverContent)getHoverInfo2(textViewer, hoverRegion);
return content != null ? content.getText() : "";
}
private String getIdentifier(IDocument doc, int pos) {
try {
char c = doc.getChar(pos);
......@@ -166,23 +186,25 @@ public final class TextHover extends BaseTextHover implements ITextHoverExtensio
return Character.isAlphabetic(c) || Character.isDigit(c) || c == '_';
}
private void addDescs(StringBuilder sb, DocumentComment dc) {
private void addDescs(StringBuilder sb, List<StyleRange> styles, DocumentComment dc) {
final List<String> descs = dc.getDescs();
if (descs.size() > 0) {
sb.append("<br><br><b>Description:</b>");
sb.append("\n\n");
addStyledText(sb, styles, "Description\n", boldRange());
for (String desc : descs) {
sb.append("<br>" + desc);
sb.append("\n" + desc);
}
}
}
private void addParams(StringBuilder sb, DocumentComment dc, Assignment ass) {
private void addParams(StringBuilder sb, List<StyleRange> styles, DocumentComment dc, Assignment ass) {
final Map<String,String> params = dc.getParams();
if (params.size() > 0) {
String paramType = null;
final Def_Function func = (Def_Function)ass;
final FormalParameterList fpl = func.getFormalParameterList();
sb.append("<br><br><b>Parameters:</b>");
sb.append("\n\n");
addStyledText(sb, styles, "Parameters:\n", boldRange());
for (Map.Entry<String, String> param : params.entrySet()) {
final FormalParameter fp = fpl.getParameterById(new Identifier(Identifier_type.ID_TTCN, param.getKey()));
if (fp != null) {
......@@ -190,44 +212,55 @@ public final class TextHover extends BaseTextHover implements ITextHoverExtensio
} else {
// FIXME : report param mismatch
}
sb.append(" <div><i><b>" + param.getKey() + "</i> (" + (paramType != null ? paramType : "<?>") + ")</b> " + param.getValue() + "</div>");
sb.append("\n ");
addStyledText(sb, styles, param.getKey(), boldItalicRange());
addStyledText(sb, styles, " (" + (paramType != null ? paramType : "<?>") + ")", boldRange());
sb.append(" " + param.getValue());
}
}
}
private void addReturn(StringBuilder sb, DocumentComment dc) {
private void addReturn(StringBuilder sb, List<StyleRange> styles, DocumentComment dc) {
final String ret = dc.getReturn();
if (ret != null) {
sb.append("<br><br><b>Returns:</b><br>");
sb.append("<pre>" + ret + "</pre>");
sb.append("\n\n");
addStyledText(sb, styles, "Returns: ", boldRange());
sb.append(ret + "\n");
}
}
private void addAuthors(StringBuilder sb, DocumentComment dc) {
private void addAuthors(StringBuilder sb, List<StyleRange> styles, DocumentComment dc) {
if (dc.getAuthors().size() > 0) {
sb.append("<br><br><b>Authors:</b><br>");
sb.append("\n\n");
addStyledText(sb, styles, "Authors:\n", boldRange());
for (String author : dc.getAuthors()) {
sb.append(" " + author + "<br>");
sb.append("\n ");
addStyledText(sb, styles, author, italicRange());
}
}
}
private void functionHover(StringBuilder sb, DocumentComment dc, Assignment ass) {
sb.append("function <b>" + ((Definition)ass).getIdentifier().getDisplayName() + "()</b>");
private void functionHover(StringBuilder sb, List<StyleRange> styles, DocumentComment dc, Assignment ass) {
sb.append("function ");
addStyledText(sb, styles, ((Definition)ass).getIdentifier().getDisplayName(), boldRange());
sb.append("()");
if (dc != null) {
addDescs(sb, dc);
addParams(sb, dc, ass);
addReturn(sb, dc);
addAuthors(sb, dc);
addDescs(sb, styles, dc);
addParams(sb, styles, dc, ass);
addReturn(sb, styles, dc);
addAuthors(sb, styles, dc);
} else {
final Def_Function func = (Def_Function)ass;
final FormalParameterList fp = func.getFormalParameterList();
if (fp.getNofParameters() > 0) {
sb.append("<br><br><b>Parameters:</b>");
sb.append("\n\n");
addStyledText(sb, styles, "Parameters\n", boldRange());
for (int i = 0; i < fp.getNofParameters(); i++) {
final FormalParameter param = fp.getParameterByIndex(i);
String paramType = getFormalParamType(param);
sb.append("<div><b><i>" + param.getIdentifier().getName() + " (</i>" + paramType + ")</b></div>");
sb.append("\n ");
addStyledText(sb, styles, param.getIdentifier().getDisplayName(), boldItalicRange());
addStyledText(sb, styles, " (" + (paramType != null ? paramType : "<?>") + ")", boldRange());
}
}
}
......@@ -250,8 +283,8 @@ public final class TextHover extends BaseTextHover implements ITextHoverExtensio
}
}
if (dc != null) {
addDescs(sb, dc);
addAuthors(sb, dc);
// addDescs(sb, dc);
// addAuthors(sb, dc);
}
}
......@@ -264,8 +297,8 @@ public final class TextHover extends BaseTextHover implements ITextHoverExtensio
sb.append("constant <b>" + typeName + ((Definition)ass).getIdentifier().getName() + "</b>");
sb.append("<br>Value: " + ((Def_Const)ass).getValue().createStringRepresentation());
if (dc != null) {
addDescs(sb, dc);
addAuthors(sb, dc);
// addDescs(sb, dc);
//addAuthors(sb, dc);
}
}
......@@ -316,28 +349,58 @@ public final class TextHover extends BaseTextHover implements ITextHoverExtensio
case A_PAR_VAL:
case A_PAR_VAL_IN:
case A_PAR_TEMP_IN:
paramType = "<i>IN</i>";
paramType = "IN";
break;
case A_PAR_VAL_INOUT:
case A_PAR_TEMP_INOUT:
paramType = "<i>INOUT</i>";
paramType = "INOUT";
break;
case A_PAR_VAL_OUT:
case A_PAR_TEMP_OUT:
paramType = "<i>OUT</i>";
paramType = "OUT";
break;
default:
paramType = null;
}
return paramType;
}
private void addStyledText(StringBuilder sb, List<StyleRange> ranges, String text, StyleRange style) {
if (style != null) {
style.start = sb.length();
style.length = text.length();
ranges.add(style);
}
sb.append(text);
}
private StyleRange boldRange() {
final StyleRange range = new StyleRange();
range.fontStyle = SWT.BOLD;
return range;
}
private StyleRange italicRange() {
final StyleRange range = new StyleRange();
range.fontStyle = SWT.ITALIC;
return range;
}
private StyleRange boldItalicRange() {
final StyleRange range = new StyleRange();
range.fontStyle = SWT.BOLD | SWT.ITALIC;
return range;
}
@Override
public IInformationControlCreator getHoverControlCreator() {
return new IInformationControlCreator() {
@Override
public IInformationControl createInformationControl(Shell parent) {
return new DefaultInformationControl(parent, EditorsUI.getTooltipAffordanceString());
return new Ttcn3HoverInfoControl(parent, EditorsUI.getTooltipAffordanceString());
}
};
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment