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

Initial implementation of missing function proposal


Signed-off-by: Miklos Magyari's avatarMiklos Magyari <miklos.magyari@sigmatechnology.se>
parent 1882922f
......@@ -9,14 +9,12 @@ package org.eclipse.titan.designer.AST.TTCN3.definitions;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.resources.IMarker;
import org.eclipse.jface.text.templates.Template;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jface.viewers.StyledString.Styler;
......
......@@ -17,6 +17,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
import org.antlr.v4.runtime.tree.ParseTree;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.titan.common.product.ProductIdentity;
import org.eclipse.titan.designer.GeneralConstants;
......@@ -36,10 +37,12 @@ import org.eclipse.titan.designer.AST.Module;
import org.eclipse.titan.designer.AST.ModuleImportation;
import org.eclipse.titan.designer.AST.ModuleImportationChain;
import org.eclipse.titan.designer.AST.NamingConventionHelper;
import org.eclipse.titan.designer.AST.ParameterisedSubReference;
import org.eclipse.titan.designer.AST.Reference;
import org.eclipse.titan.designer.AST.ReferenceFinder;
import org.eclipse.titan.designer.AST.ReferenceFinder.Hit;
import org.eclipse.titan.designer.AST.Scope;
import org.eclipse.titan.designer.AST.Type;
import org.eclipse.titan.designer.AST.TTCN3.attributes.AttributeSpecification;
import org.eclipse.titan.designer.AST.TTCN3.attributes.ExtensionAttribute;
import org.eclipse.titan.designer.AST.TTCN3.attributes.ModuleVersionAttribute;
......@@ -47,6 +50,9 @@ import org.eclipse.titan.designer.AST.TTCN3.attributes.MultipleWithAttributes;
import org.eclipse.titan.designer.AST.TTCN3.attributes.Qualifiers;
import org.eclipse.titan.designer.AST.TTCN3.attributes.SingleWithAttribute;
import org.eclipse.titan.designer.AST.TTCN3.attributes.SingleWithAttribute.Attribute_Type;
import org.eclipse.titan.designer.AST.TTCN3.templates.ParsedActualParameters;
import org.eclipse.titan.designer.AST.TTCN3.templates.TemplateInstance;
import org.eclipse.titan.designer.AST.TTCN3.templates.TemplateInstances;
import org.eclipse.titan.designer.AST.TTCN3.attributes.TitanVersionAttribute;
import org.eclipse.titan.designer.AST.TTCN3.attributes.VersionRequirementAttribute;
import org.eclipse.titan.designer.AST.TTCN3.attributes.WithAttributesPath;
......@@ -59,6 +65,7 @@ import org.eclipse.titan.designer.editors.ProposalCollector;
import org.eclipse.titan.designer.editors.SkeletonTemplateProposal;
import org.eclipse.titan.designer.editors.T3Doc;
import org.eclipse.titan.designer.editors.actions.DeclarationCollector;
import org.eclipse.titan.designer.editors.controls.HoverProposal;
import org.eclipse.titan.designer.editors.ttcn3editor.TTCN3CodeSkeletons;
import org.eclipse.titan.designer.editors.ttcn3editor.TTCN3Keywords;
import org.eclipse.titan.designer.parsers.CompilationTimeStamp;
......@@ -118,6 +125,8 @@ public final class TTCN3Module extends Module {
private boolean needsTobeBuilt = true;
private final static int PROTOTYPE_INDEX = -1;
public TTCN3Module(final Identifier identifier, final IProject project) {
super(identifier, project);
......@@ -855,6 +864,41 @@ public final class TTCN3Module extends Module {
if (temporalAssignment != null) {
return temporalAssignment;
}
final List<ISubReference> subReferences = reference.getSubreferences();;
if (subReferences.size() > 0) {
if (subReferences.get(0) instanceof ParameterisedSubReference) {
StringBuilder propCode = new StringBuilder();
propCode.append("\n\tfunction ");
propCode.append(reference.getDisplayName().substring(0, reference.getDisplayName().length() - 1));
final ParameterisedSubReference subref = (ParameterisedSubReference)subReferences.get(0);
final ParsedActualParameters parsed = subref.getParsedParameters();
TemplateInstances instances = parsed.getInstances();
for (int i = 0; i < instances.getNofTis(); i++) {
final TemplateInstance inst = instances.getInstanceByIndex(i);
if (i > 0 ) {
propCode.append(", ");
}
propCode.append(Type.typeToSrcType(inst.getExpressionReturntype(timestamp, null)));
propCode.append(" param" + (i + 1));
}
propCode.append(") {\n\t\t// function body\n\t}\n\n");
HoverProposal prop = new HoverProposal("Insert missing function prototype...") {
@Override
public void run(IMarker marker) {
insertTextAtDefs(PROTOTYPE_INDEX);
}
};
prop.addProposal(PROTOTYPE_INDEX, propCode.toString());
referenceLocation
.reportSemanticError(MessageFormat.format(MISSINGREFERENCE, id.getDisplayName(), identifier.getDisplayName()),
new HoverProposal[] {
prop
});
missingReferences.add(reference);
return temporalAssignment;
}
}
referenceLocation
.reportSemanticError(MessageFormat.format(MISSINGREFERENCE, id.getDisplayName(), identifier.getDisplayName()));
......
......@@ -24,6 +24,7 @@ import org.eclipse.titan.common.parsers.CharstringExtractor;
import org.eclipse.titan.designer.Activator;
import org.eclipse.titan.designer.GeneralConstants;
import org.eclipse.titan.designer.AST.ISubReference.Subreference_type;
import org.eclipse.titan.designer.AST.IType.Type_type;
import org.eclipse.titan.designer.AST.IValue.Value_type;
import org.eclipse.titan.designer.AST.ReferenceFinder.Hit;
import org.eclipse.titan.designer.AST.ASN1.Value_Assignment;
......@@ -4179,4 +4180,26 @@ public abstract class Type extends Governor implements IType, IIncrementallyUpda
// the default implementation does nothing
return fromName;
}
/**
* Gets the name of types for quick fix code insertions
* FIXME add missing types
*
* @param type
* @return
*/
public static String typeToSrcType(Type_type type) {
switch (type) {
case TYPE_INTEGER:
return "integer";
case TYPE_CHARSTRING:
return "charstring";
case TYPE_BITSTRING:
return "bitstring";
case TYPE_REAL:
return "float";
default:
return "";
}
}
}
......@@ -9,6 +9,8 @@ package org.eclipse.titan.designer.editors.controls;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
......@@ -19,7 +21,11 @@ import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.titan.common.logging.ErrorReporter;
import org.eclipse.titan.designer.AST.Location;
import org.eclipse.titan.designer.AST.Module;
import org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module;
import org.eclipse.titan.designer.editors.ttcn3editor.TTCN3Editor;
import org.eclipse.titan.designer.parsers.GlobalParser;
import org.eclipse.titan.designer.parsers.ProjectSourceParser;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.views.markers.WorkbenchMarkerResolution;
......@@ -38,6 +44,11 @@ public class HoverProposal extends WorkbenchMarkerResolution {
private Location location;
private int offset;
private int indent;
/**
* Map of offset/string pairs to be inserted into the document
*
*/
private Map<Integer, String> inserts = new HashMap<Integer, String>();
public HoverProposal(String label) {
......@@ -54,7 +65,7 @@ public class HoverProposal extends WorkbenchMarkerResolution {
public HoverProposal(String label, int offset, String textToInsert) {
this.label = label;
this.inserts.put(offset, textToInsert);
}
}
/**
* Creates a proposal with multiple strings to be inserted at different locations
......@@ -67,6 +78,17 @@ public class HoverProposal extends WorkbenchMarkerResolution {
this.inserts = textsToInsert;
}
/**
* Adds a proposal with multiple strings to be inserted at different locations
*
* @param offset Offset of the document position where the proposed text should be inserted
* @param textToInsert Map of positions and texts to be inserted
*
*/
public void addProposal(int offset, String textToInsert) {
this.inserts.put(offset, textToInsert);
}
@Override
public String getLabel() {
return label;
......@@ -107,10 +129,23 @@ public class HoverProposal extends WorkbenchMarkerResolution {
}
}
/**
* Inserts text at the end of definition section of the current file
*
* In this case, offset is not an offset in the document, but acts as a key to look up the string in the map of inserts
*
* @param offsetKey index of string to be inserted
*/
public void insertTextAtDefs(int offsetKey) {
final int offset = getDefinitionsLocation();
insertText(inserts.get(offsetKey), offset);
}
private void getActiveEditor() {
editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
}
// FIXME this is completely unfinished
protected void calculateIndent() {
getActiveEditor();
IDocument doc = ((TTCN3Editor) editor).getDocument();
......@@ -132,6 +167,15 @@ public class HoverProposal extends WorkbenchMarkerResolution {
this.indent = indent;
}
public int getDefinitionsLocation() {
getActiveEditor();
final IFile file = (IFile) editor.getEditorInput().getAdapter(IFile.class);
final ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(file.getProject());
final Module actualModule = projectSourceParser.containedModule(file);
return ((TTCN3Module) actualModule).getAssignmentsScope().getLocation().getOffset() + 1;
}
protected void insertText(String text, int newOffset) {
getActiveEditor();
......
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