Commit 1882922f authored by Miklos Magyari's avatar Miklos Magyari
Browse files

Proposal improvements


Signed-off-by: Miklos Magyari's avatarMiklos Magyari <miklos.magyari@sigmatechnology.se>
parent c4d86192
......@@ -9,6 +9,7 @@ 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;
......@@ -610,27 +611,22 @@ public final class Def_Function extends Definition implements IParameterisedAssi
// check the presence of return statements
switch (block.hasReturn(timestamp)) {
case RS_NO:
String retval = "";
switch (getType(timestamp).getTypetypeTtcn3()) {
case TYPE_INTEGER:
retval = "0";
break;
case TYPE_CHARSTRING:
retval = "\"\"";
break;
case TYPE_HEXSTRING:
retval = "00H";
default:
}
identifier.getLocation().reportSemanticError(
"The function has a return type, but it does not have any return statement",
new HoverProposal[] {
new HoverProposal("Add missing return statement...", getLocation().getEndOffset() - 1) {
@Override
public void run(IMarker marker) {
String retval = "";
switch (getType(timestamp).getTypetypeTtcn3()) {
case TYPE_INTEGER:
retval = "0";
break;
case TYPE_CHARSTRING:
retval = "\"\"";
break;
case TYPE_HEXSTRING:
retval = "00H";
default:
}
insertText("return " + retval + ";\n");
}
}
new HoverProposal("Add missing return statement...", getLocation().getEndOffset() - 1, "return " + retval + ";\n"),
});
break;
case RS_MAYBE:
......
......@@ -7,6 +7,8 @@
******************************************************************************/
package org.eclipse.titan.designer.editors.controls;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.resources.IMarker;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
......@@ -19,8 +21,15 @@ import org.eclipse.titan.common.logging.ErrorReporter;
import org.eclipse.titan.designer.AST.Location;
import org.eclipse.titan.designer.editors.ttcn3editor.TTCN3Editor;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.views.markers.WorkbenchMarkerResolution;
/**
* This class handles proposal data for markers
*
* @author Miklos Magyari
*
*/
public class HoverProposal extends WorkbenchMarkerResolution {
public static final String PROPOSAL = "proposal";
......@@ -29,10 +38,33 @@ public class HoverProposal extends WorkbenchMarkerResolution {
private Location location;
private int offset;
private int indent;
private Map<Integer, String> inserts = new HashMap<Integer, String>();
public HoverProposal(String label, int offset) {
public HoverProposal(String label) {
this.label = label;
this.offset = offset;
}
/**
* Creates a proposal with one string to be inserted at a certain location
*
* @param label Text that will be shown in the list of available proposals
* @param offset Offset of the document positin where the proposed text should be inserted
* @param textToInsert Text to be inserted
*/
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
*
* @param label Text that will be shown in the list of available proposals
* @param textToInsert Map of positions and texts to be inserted
*/
public HoverProposal(String label, Map<Integer, String> textsToInsert) {
this.label = label;
this.inserts = textsToInsert;
}
@Override
......@@ -41,8 +73,14 @@ public class HoverProposal extends WorkbenchMarkerResolution {
}
@Override
/**
* Default action for the proposal
*
* If not overridden, it inserts all texts at their corresponding offsets
*
*/
public void run(IMarker marker) {
insertText();
}
@Override
......@@ -59,42 +97,52 @@ public class HoverProposal extends WorkbenchMarkerResolution {
@Override
public IMarker[] findOtherMarkers(IMarker[] markers) {
// TODO Auto-generated method stub
return null;
// FIXME
return new IMarker[] {};
}
public void setEditor(IEditorPart editor) {
this.editor = editor;
countIndent();
protected void insertText() {
for (Map.Entry<Integer, String> insert : inserts.entrySet()) {
insertText(insert.getValue(), insert.getKey());
}
}
protected void insertText(String text) {
insertText(text, offset);
private void getActiveEditor() {
editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
}
protected int countIndent() {
protected void calculateIndent() {
getActiveEditor();
IDocument doc = ((TTCN3Editor) editor).getDocument();
int indent = 0;
try {
while (offset > indent && doc.getChar(offset - indent) == '\t') {
indent++;
while (offset > 0) {
char c = doc.getChar(offset - indent);
if (c == '\t') {
indent += 4;
} else if (c == ' ') {
indent++;
} else {
break;
}
}
} catch (BadLocationException e) {
}
this.indent = indent;
return indent;
}
protected void insertText(String text, int newOffset) {
if (editor == null) {
return;
}
getActiveEditor();
StringBuilder indentedText = new StringBuilder();
for (int i = 0; i <= indent; i++) {
for (int i = 0; i < indent / 4; i++) {
indentedText.append('\t');
}
for (int i = 0; i < indent % 4; i++) {
indentedText.append(' ');
}
indentedText.append('\t');
indentedText.append(text);
final MultiTextEdit multiEdit = new MultiTextEdit(newOffset, 0);
final RewriteSessionEditProcessor processor = new RewriteSessionEditProcessor(((TTCN3Editor) editor).getDocument(),
......
......@@ -134,7 +134,6 @@ public final class TextHover extends BaseTextHover implements ITextHoverExtensio
HoverProposal[] propsInMarker = (HoverProposal[])attr;
for (int i = 0; i < propsInMarker.length; i++) {
HoverProposal propEntry = propsInMarker[i];
propEntry.setEditor(editor);
proposals.add(propEntry);
}
}
......
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