Commit 087902b5 authored by petter's avatar petter

Collapses blocks of code

parent ab07e0b7
......@@ -17,6 +17,7 @@ Import-Package: org.eclipse.jface.text,
org.eclipse.jface.text.presentation,
org.eclipse.jface.text.rules,
org.eclipse.jface.text.source,
org.eclipse.jface.text.source.projection,
org.eclipse.ui.editors.text,
org.eclipse.ui.views.contentoutline
Bundle-ClassPath: java-cup-11b-runtime.jar,
......
package mjplugin.ast;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.jface.text.Position;
import minijava.MinijavaVisitor;
import minijava.Stmt.Compound;
public class Collapsables extends MinijavaVisitor {
List<Position> list = new LinkedList<>();
public List<Position> getThem(){
return list;
}
@Override
public void postVisit(Compound i) {
list.add(new Position(i.getLeft().getOffset(),i.getRight().getOffset()-i.getLeft().getOffset()));
}
}
package mjplugin.ast;
import java.util.List;
import java.util.Map;
import org.eclipse.jface.text.Position;
import minijava.Decl;
import minijava.Expr.Identifier;
import minijava.Program;
public class MiniJavaAST {
public Program program;
private Map<Identifier, Decl> bindings;
public MiniJavaAST(Program program,Map<Identifier, Decl> bindings) {
private List<Position> collapsables;
public MiniJavaAST(Program program,Map<Identifier, Decl> bindings, List<Position> them) {
this.program = program;
this.bindings=bindings;
this.collapsables=them;
}
public Decl getBinding(Identifier id){
return bindings.get(id);
}
public List<Position> getCollapsables(){
return collapsables;
}
}
......@@ -10,6 +10,7 @@ import java.util.List;
import java_cup.runtime.ComplexSymbolFactory.Location;
import minijava.Program;
import miniparser.InMemoryParser;
import mjplugin.ast.Collapsables;
import mjplugin.ast.DeclarationUse;
import mjplugin.ast.MiniJavaAST;
import mjplugin.controller.Controller.JobsToDo;
......@@ -86,7 +87,9 @@ public class DocumentDidChangeJob extends Job {
Program program = parser.parse();
DeclarationUse typing = new DeclarationUse(reporter);
program.accept(typing);
result = new MiniJavaAST(program,typing.getBindings());
Collapsables collapsables = new Collapsables();
program.accept(collapsables);
result = new MiniJavaAST(program,typing.getBindings(),collapsables.getThem());
}catch (InMemoryParser.NoRecoveryException nre){
hasParserErrors = true;
}catch(ClassCastException cce){
......
......@@ -60,7 +60,7 @@ public class GUIErrorReporter implements IFrontendObserver {
}
@Override
public void report(String message, Location left, Location right, List expected) {
public void report(String message, Location left, Location right, List expect) {
errors.add(new Message(message, left, right));
}
......
package mjplugin.editors;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java_cup.runtime.ComplexSymbolFactory.Location;
import mjplugin.controller.Controller;
......@@ -13,10 +16,19 @@ import mjplugin.model.Model;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.AnnotationModel;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.jface.text.source.projection.ProjectionAnnotation;
import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel;
import org.eclipse.jface.text.source.projection.ProjectionSupport;
import org.eclipse.jface.text.source.projection.ProjectionViewer;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
......@@ -78,6 +90,45 @@ public class MJEditor extends TextEditor implements IMJParserChangeObserver,IReg
public void endCompoundChange() {
((SourceViewer) getSourceViewer()).getUndoManager().endCompoundChange();
}
/**
* BEGIN Folding
*/
private ProjectionAnnotationModel annotationModel;
@Override
public void createPartControl(Composite parent) {
super.createPartControl(parent);
ProjectionViewer viewer = (ProjectionViewer)getSourceViewer();
ProjectionSupport projectionSupport = new ProjectionSupport(viewer, getAnnotationAccess(), getSharedColors());
projectionSupport.install();
viewer.doOperation(ProjectionViewer.TOGGLE);
annotationModel = viewer.getProjectionAnnotationModel();
}
@Override
protected ISourceViewer createSourceViewer(Composite parent,IVerticalRuler ruler, int styles) {
ISourceViewer viewer = new ProjectionViewer(parent, ruler, getOverviewRuler(), isOverviewRulerVisible(), styles);
getSourceViewerDecorationSupport(viewer);
return viewer;
}
private Annotation[] oldAnnotations;
public void updateFoldingStructure(List<Position> positions)
{
Annotation[] annotations = new Annotation[positions.size()];
Map<Annotation,Position> newAnnotations = new HashMap<>();
for(int i = 0; i < positions.size();i++)
{
ProjectionAnnotation annotation = new ProjectionAnnotation();
newAnnotations.put(annotation, positions.get(i));
annotations[i] = annotation;
}
annotationModel.modifyAnnotations(oldAnnotations, newAnnotations,null);
oldAnnotations = annotations;
}
/**
* END Folding
*/
@Override
public void init(IEditorSite site, IEditorInput input)
throws PartInitException {
......@@ -123,7 +174,9 @@ public class MJEditor extends TextEditor implements IMJParserChangeObserver,IReg
private MJOutlinePage myOutline;
@Override
public void modelChanged(Model model) {
updateFoldingStructure(model.getAstModel().getCollapsables());
if (hasRequestedPostSaveSyntaxRefresh) {
getSourceViewer().invalidateTextPresentation();
hasRequestedPostSaveSyntaxRefresh = false;
}
......
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