static void addCreateProposal( Collection<ICompletionProposal> proposals, String def, boolean local, String desc, Image image, PhasedUnit unit, Tree.Statement statement, ProducedType t) { IFile file = CeylonBuilder.getFile(unit); TextFileChange change = new TextFileChange(local ? "Create Local" : "Create Toplevel", file); change.setEdit(new MultiTextEdit()); IDocument doc = CreateProposal.getDocument(change); String indent = CeylonQuickFixAssistant.getIndent(statement, doc); int offset = statement.getStartIndex(); def = def.replace("$indent", indent); int il = importType(change, t, unit.getCompilationUnit()); change.addEdit(new InsertEdit(offset, def + "\n" + indent)); proposals.add( new CreateProposal( def, (local ? "Create local " : "Create toplevel ") + desc, image, 0, offset + il, file, change)); }
private static void addCreateProposal( Collection<ICompletionProposal> proposals, boolean local, DefinitionGenerator dg, PhasedUnit unit, Tree.Statement statement) { IFile file = getFile(unit); TextFileChange change = new TextFileChange(local ? "Create Local" : "Create Toplevel", file); change.setEdit(new MultiTextEdit()); IDocument doc = EditorUtil.getDocument(change); String indent = getIndent(statement, doc); int offset = statement.getStartIndex(); String delim = getDefaultLineDelimiter(doc); Tree.CompilationUnit cu = unit.getCompilationUnit(); int il = applyImports(change, dg.getImports(), cu, doc); String def = dg.generate(indent, delim) + delim + indent; if (!local) def += delim; change.addEdit(new InsertEdit(offset, def)); String desc = (local ? "Create local " : "Create toplevel ") + dg.getDescription(); final Scope scope = local ? statement.getScope() : cu.getUnit().getPackage(); int exitPos = dg.getNode().getStopIndex() + 1; proposals.add( new CreateProposal( def, desc, scope, cu.getUnit(), dg.getReturnType(), dg.getImage(), offset + il, change, exitPos, dg instanceof ObjectClassDefinitionGenerator)); }
private List<Statement> getStatements(Tree.Body body, ITextSelection selection) { List<Statement> statements = new ArrayList<Statement>(); for (Tree.Statement s : body.getStatements()) { if (s.getStartIndex() >= selection.getOffset() && s.getStopIndex() <= selection.getOffset() + selection.getLength()) { statements.add(s); } } return statements; }
static void addCreateMemberProposal( Collection<ICompletionProposal> proposals, String def, String desc, Image image, Declaration typeDec, PhasedUnit unit, Tree.Declaration decNode, Tree.Body body, ProducedType t) { IFile file = CeylonBuilder.getFile(unit); TextFileChange change = new TextFileChange("Create Member", file); change.setEdit(new MultiTextEdit()); IDocument doc = CreateProposal.getDocument(change); String indent; String indentAfter; int offset; List<Tree.Statement> statements = body.getStatements(); if (statements.isEmpty()) { indentAfter = "\n" + CeylonQuickFixAssistant.getIndent(decNode, doc); indent = indentAfter + getDefaultIndent(); offset = body.getStartIndex() + 1; } else { Tree.Statement statement = statements.get(statements.size() - 1); indent = ""; offset = statement.getStartIndex(); indentAfter = "\n" + CeylonQuickFixAssistant.getIndent(statement, doc); } int il = importType(change, t, unit.getCompilationUnit()); change.addEdit(new InsertEdit(offset, indent + def + indentAfter)); proposals.add( new CreateProposal( def, "Create " + desc + " in '" + typeDec.getName() + "'", image, indent.length(), offset + il, file, change)); }
static void addCreateMemberProposal( Collection<ICompletionProposal> proposals, DefinitionGenerator dg, Declaration typeDec, PhasedUnit unit, Tree.Declaration decNode, Tree.Body body, Tree.Statement statement) { IFile file = getFile(unit); TextFileChange change = new TextFileChange("Create Member", file); change.setEdit(new MultiTextEdit()); IDocument doc = EditorUtil.getDocument(change); String indentBefore; String indentAfter; String indent; int offset; List<Tree.Statement> statements = body.getStatements(); String delim = getDefaultLineDelimiter(doc); if (statements.isEmpty()) { String bodyIndent = getIndent(decNode, doc); indent = bodyIndent + getDefaultIndent(); indentBefore = delim + indent; try { boolean singleLineBody = doc.getLineOfOffset(body.getStartIndex()) == doc.getLineOfOffset(body.getStopIndex()); if (singleLineBody) { indentAfter = delim + bodyIndent; } else { indentAfter = ""; } } catch (BadLocationException e) { e.printStackTrace(); indentAfter = delim; } offset = body.getStartIndex() + 1; } else { Tree.Statement st; if (statement != null && statement.getUnit().equals(body.getUnit()) && statement.getStartIndex() >= body.getStartIndex() && statement.getStopIndex() <= body.getStopIndex()) { st = statements.get(0); for (Tree.Statement s : statements) { if (statement.getStartIndex() >= s.getStartIndex() && statement.getStopIndex() <= s.getStopIndex()) { st = s; } } indent = getIndent(st, doc); indentBefore = ""; indentAfter = delim + indent; offset = st.getStartIndex(); } else { st = statements.get(statements.size() - 1); indent = getIndent(st, doc); indentBefore = delim + indent; indentAfter = ""; offset = st.getStopIndex() + 1; } } String generated = typeDec instanceof Interface ? dg.generateSharedFormal(indent, delim) : dg.generateShared(indent, delim); String def = indentBefore + generated + indentAfter; int il = applyImports(change, dg.getImports(), unit.getCompilationUnit(), doc); change.addEdit(new InsertEdit(offset, def)); String desc = "Create " + memberKind(dg) + " in '" + typeDec.getName() + "'"; int exitPos = dg.getNode().getStopIndex() + 1; proposals.add( new CreateProposal( def, desc, body.getScope(), body.getUnit(), dg.getReturnType(), dg.getImage(), offset + il, change, exitPos, dg instanceof ObjectClassDefinitionGenerator)); }