use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.
the class LinkedNamesAssistProposal method apply.
/* (non-Javadoc)
* @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
*/
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
try {
Point seletion = viewer.getSelectedRange();
// get full ast
CompilationUnit root = SharedASTProvider.getAST(fContext.getCompilationUnit(), SharedASTProvider.WAIT_YES, null);
ASTNode nameNode = NodeFinder.perform(root, fNode.getStartPosition(), fNode.getLength());
final int pos = fNode.getStartPosition();
ASTNode[] sameNodes;
if (nameNode instanceof SimpleName) {
sameNodes = LinkedNodeFinder.findByNode(root, (SimpleName) nameNode);
} else {
sameNodes = new ASTNode[] { nameNode };
}
// sort for iteration order, starting with the node @ offset
Arrays.sort(sameNodes, new Comparator<ASTNode>() {
public int compare(ASTNode o1, ASTNode o2) {
return rank(o1) - rank(o2);
}
/**
* Returns the absolute rank of an <code>ASTNode</code>. Nodes
* preceding <code>offset</code> are ranked last.
*
* @param node the node to compute the rank for
* @return the rank of the node with respect to the invocation offset
*/
private int rank(ASTNode node) {
int relativeRank = node.getStartPosition() + node.getLength() - pos;
if (relativeRank < 0)
return Integer.MAX_VALUE + relativeRank;
else
return relativeRank;
}
});
IDocument document = viewer.getDocument();
LinkedPositionGroupImpl group = new LinkedPositionGroupImpl();
for (int i = 0; i < sameNodes.length; i++) {
ASTNode elem = sameNodes[i];
RegionImpl region = new RegionImpl();
region.setOffset(elem.getStartPosition());
region.setLength(elem.getLength());
group.addPositions(region);
// group.addPosition(new LinkedPosition(document, elem.getStartPosition(), elem.getLength(), i));
}
LinkedModeModelImpl model = new LinkedModeModelImpl();
model.addGroups(group);
// model.forceInstall();
model.setEscapePosition(offset);
this.linkedModel = model;
if (fContext instanceof AssistContext) {
// IEditorPart editor = ((AssistContext)fContext).getEditor();
// if (editor instanceof JavaEditor) {
// model.addLinkingListener(new EditorHighlightingSynchronizer((JavaEditor)editor));
// }
}
if (fValueSuggestion != null) {
document.replace(nameNode.getStartPosition(), nameNode.getLength(), fValueSuggestion);
// IRegion selectedRegion = ui.getSelectedRegion();
// seletion = new Point(selectedRegion.getOffset(), fValueSuggestion.length());
}
// viewer.setSelectedRange(seletion.x, seletion.y); // by default full word is selected, restore original selection
} catch (BadLocationException e) {
JavaPlugin.log(e);
}
}
use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.
the class AssistQuickFixTest17 method testUnrollMultiCatch2.
@Test
public void testUnrollMultiCatch2() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" void foo() {\n");
buf.append(" try {\n");
buf.append(" System.out.println(\"foo\");\n");
buf.append(" } catch (IllegalArgumentException | NullPointerException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" } catch (RuntimeException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
int offset = buf.toString().indexOf("catch");
AssistContext context = getCorrectionContext(cu, offset, 0);
assertNoErrors(context);
List proposals = collectAssists(context, false);
assertCorrectLabels(proposals);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" void foo() {\n");
buf.append(" try {\n");
buf.append(" System.out.println(\"foo\");\n");
buf.append(" } catch (IllegalArgumentException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" } catch (NullPointerException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" } catch (RuntimeException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected1 = buf.toString();
assertExpectedExistInProposals(proposals, new String[] { expected1 });
}
use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.
the class AssistQuickFixTest17 method testPickoutTypeFromMulticatch2.
@Test
public void testPickoutTypeFromMulticatch2() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.lang.reflect.InvocationTargetException;\n");
buf.append("public class E {\n");
buf.append(" public void foo() {\n");
buf.append(" try {\n");
buf.append(" String.class.getConstructor().newInstance();\n");
buf.append(" } catch (InstantiationException | IllegalAccessException\n");
buf.append(" | IllegalArgumentException | InvocationTargetException\n");
buf.append(" | java.lang.NoSuchMethodException | SecurityException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
String string = "MethodException";
int offset = buf.toString().indexOf(string);
AssistContext context = getCorrectionContext(cu, offset, 0);
List proposals = collectAssists(context, false);
assertNumberOfProposals(proposals, 4);
assertCorrectLabels(proposals);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.lang.reflect.InvocationTargetException;\n");
buf.append("public class E {\n");
buf.append(" public void foo() {\n");
buf.append(" try {\n");
buf.append(" String.class.getConstructor().newInstance();\n");
buf.append(" } catch (InstantiationException | IllegalAccessException\n");
buf.append(" | IllegalArgumentException | InvocationTargetException\n");
buf.append(" | SecurityException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected1 = buf.toString();
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.lang.reflect.InvocationTargetException;\n");
buf.append("public class E {\n");
buf.append(" public void foo() throws java.lang.NoSuchMethodException {\n");
buf.append(" try {\n");
buf.append(" String.class.getConstructor().newInstance();\n");
buf.append(" } catch (InstantiationException | IllegalAccessException\n");
buf.append(" | IllegalArgumentException | InvocationTargetException\n");
buf.append(" | SecurityException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected2 = buf.toString();
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.lang.reflect.InvocationTargetException;\n");
buf.append("public class E {\n");
buf.append(" public void foo() {\n");
buf.append(" try {\n");
buf.append(" String.class.getConstructor().newInstance();\n");
buf.append(" } catch (InstantiationException | IllegalAccessException\n");
buf.append(" | IllegalArgumentException | InvocationTargetException\n");
buf.append(" | SecurityException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" } catch (java.lang.NoSuchMethodException e) {\n");
buf.append(" e.printStackTrace();\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected3 = buf.toString();
assertExpectedExistInProposals(proposals, new String[] { expected1, expected2, expected3 });
}
use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.
the class AssistQuickFixTest18 method testConvertToAnonymousClassCreation1.
@Test
public void testConvertToAnonymousClassCreation1() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("interface I {\n");
buf.append(" void method();\n");
buf.append("}\n");
buf.append("public class E {\n");
buf.append(" void bar(I i) {\n");
buf.append(" }\n");
buf.append(" void foo() {\n");
buf.append(" bar(() -> {\n");
buf.append(" System.out.println();\n");
buf.append(" System.out.println();\n");
buf.append(" });\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
int offset = buf.toString().indexOf("->");
AssistContext context = getCorrectionContext(cu, offset, 0);
assertNoErrors(context);
List proposals = collectAssists(context, false);
assertNumberOfProposals(proposals, 4);
assertCorrectLabels(proposals);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("interface I {\n");
buf.append(" void method();\n");
buf.append("}\n");
buf.append("public class E {\n");
buf.append(" void bar(I i) {\n");
buf.append(" }\n");
buf.append(" void foo() {\n");
buf.append(" bar(new I() {\n");
buf.append(" @Override\n");
buf.append(" public void method() {\n");
buf.append(" System.out.println();\n");
buf.append(" System.out.println();\n");
buf.append(" }\n");
buf.append(" });\n");
buf.append(" }\n");
buf.append("}\n");
String expected1 = buf.toString();
assertExpectedExistInProposals(proposals, new String[] { expected1 });
}
use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.
the class AssistQuickFixTest18 method testAssignParamToField2.
@Test
public void testAssignParamToField2() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("public interface I {\n");
buf.append(" static void bar(int x) {\n");
buf.append(" System.out.println(x);\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("I.java", buf.toString(), false, null);
int offset = buf.toString().indexOf("x");
AssistContext context = getCorrectionContext(cu, offset, 0);
assertNoErrors(context);
List proposals = collectAssists(context, false);
assertNumberOfProposals(proposals, 0);
}
Aggregations