Commit a8fb2740 authored by Sebastian Pretscher's avatar Sebastian Pretscher

Commit early/often...

parent 142978c2
......@@ -13,10 +13,12 @@ import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.ui.texteditor.IDocumentProvider;
import de.tum.in.www2.cupplugin.controller.Controller.JobsToDo;
import de.tum.in.www2.cupplugin.debug.Debugger;
import de.tum.in.www2.cupplugin.editors.CupPartionScanner;
import de.tum.in.www2.cupplugin.editors.CupTextEditor;
import de.tum.in.www2.cupplugin.editors.RevisionManager;
import de.tum.in.www2.cupplugin.model.Model;
public class Controller {
......@@ -28,7 +30,7 @@ public class Controller {
static final int SET_CHANGED_AFTER_SECONDS = 1;
// TODO: we need to remove the editor from the list, when it gets disposed.
// Static reference to the controller instance per document
static WeakHashMap<CupTextEditor, Controller> instances = new WeakHashMap<CupTextEditor, Controller>();
static CupTextEditor AnonymousEditor = null;
......@@ -46,9 +48,11 @@ public class Controller {
private List<DocumentEvent> documentEvents;
private final EnumSet<JobsToDo> jobElements = EnumSet
.noneOf(JobsToDo.class);
private long currentRevisionNumber = 0;
private List<RegisterForControllerChanges> controllerObservers;
/*
* Static singleton getter
*/
......@@ -68,11 +72,12 @@ public class Controller {
if (editor != null) {
IDocumentProvider provider = editor.getDocumentProvider();
if (provider != null) {
IDocument doc2 = provider.getDocument(editor.getEditorInput());
IDocument doc2 = provider.getDocument(editor
.getEditorInput());
if (doc2.equals(doc))
return e.getValue();
}
}
}
// this is hacky, but it shouldn't happen anyways...
......@@ -88,6 +93,7 @@ public class Controller {
myJob.setSystem(true);
documentEvents = new LinkedList<DocumentEvent>();
Debugger.getInstance(editor);
controllerObservers = new LinkedList<RegisterForControllerChanges>();
}
public void addDocumentEvent(DocumentEvent e) {
......@@ -110,6 +116,12 @@ public class Controller {
}
}
public void removeJobToDo(JobsToDo job) {
synchronized (jobElements) {
jobElements.remove(job);
}
}
public EnumSet<JobsToDo> popJobsToDo() {
synchronized (jobElements) {
final EnumSet<JobsToDo> popJobs = EnumSet.noneOf(JobsToDo.class);
......@@ -124,7 +136,13 @@ public class Controller {
addJobToDo(JobsToDo.buildTable);
myJob.schedule(0);
}
public void registerObserver(RegisterForControllerChanges observer) {
synchronized (controllerObservers) {
controllerObservers.add(observer);
}
}
/*
* notify method that's to be called, when a change in the document happens
*/
......@@ -137,71 +155,85 @@ public class Controller {
myEditor.getEditorInput());
currentRevisionNumber = RevisionManager.increment(document);
EnumSet<JobsToDo> observedJobs = EnumSet.noneOf(JobsToDo.class);
synchronized (controllerObservers) {
for (RegisterForControllerChanges observer : controllerObservers) {
observedJobs.addAll(observer.getRequiredJobs());
}
}
EnumSet<JobsToDo> possibleJobs = EnumSet.noneOf(JobsToDo.class);
try {
ITypedRegion region = document.getPartition(event.getOffset());
//TODO: Space for performance improvements
if (event.getText().trim().length() == 0 && (document.getLength()<=(event.getOffset()+1) || document.getChar(event.getOffset()+1) == (' '))) {
// TODO: Space for performance improvements
if (event.getText().trim().length() == 0
&& (document.getLength() <= (event.getOffset() + 1) || document
.getChar(event.getOffset() + 1) == (' '))) {
if (event.getText().equals(
System.getProperty("line.separator"))) {
// Some debug outut...
addJobToDo(JobsToDo.locationPatch);
possibleJobs.add(JobsToDo.locationPatch);
}
if (event.getText().equals("")) {
char beforeDelete = '+'; // DUMMY
int offset = event.getOffset();
if (offset > 0)
beforeDelete = document.get().charAt(offset - 1);
if (region.getType().equals(CupPartionScanner.JAVA_CODE)) {
beforeDelete = document.get().charAt(offset - 1);
if (region.getType()
.equals(CupPartionScanner.JAVA_CODE)) {
if (beforeDelete == '{') {
addJobToDo(JobsToDo.parseCode);
addJobToDo(JobsToDo.buildTable);
possibleJobs.add(JobsToDo.parseCode);
possibleJobs.add(JobsToDo.buildTable);
}
} else if (region.getType().equals(CupPartionScanner.SINGLE_COMMENT)){
} else if (region.getType().equals(
CupPartionScanner.SINGLE_COMMENT)) {
if (beforeDelete == '/') {
addJobToDo(JobsToDo.parseCode);
addJobToDo(JobsToDo.buildTable);
possibleJobs.add(JobsToDo.parseCode);
possibleJobs.add(JobsToDo.buildTable);
}
} else if (region.getType().equals(CupPartionScanner.MULTILINE_COMMENT)){
} else if (region.getType().equals(
CupPartionScanner.MULTILINE_COMMENT)) {
if (beforeDelete == '/' || beforeDelete == '*') {
addJobToDo(JobsToDo.parseCode);
addJobToDo(JobsToDo.buildTable);
possibleJobs.add(JobsToDo.parseCode);
possibleJobs.add(JobsToDo.buildTable);
}
} else {
addJobToDo(JobsToDo.parseCode);
addJobToDo(JobsToDo.buildTable);
possibleJobs.add(JobsToDo.parseCode);
possibleJobs.add(JobsToDo.buildTable);
}
}
} else {
String regionType = region.getType();
switch (regionType) {
case CupPartionScanner.JAVA_CODE:
if(event.getText().equals("{")) {
addJobToDo(JobsToDo.parseCode);
addJobToDo(JobsToDo.buildTable);
if (event.getText().equals("{")) {
possibleJobs.add(JobsToDo.parseCode);
possibleJobs.add(JobsToDo.buildTable);
} else {
addJobToDo(JobsToDo.locationPatch);
possibleJobs.add(JobsToDo.locationPatch);
}
break;
case CupPartionScanner.MULTILINE_COMMENT:
if(event.getText().equals("/")) {
addJobToDo(JobsToDo.parseCode);
addJobToDo(JobsToDo.buildTable);
if (event.getText().equals("/")) {
possibleJobs.add(JobsToDo.parseCode);
possibleJobs.add(JobsToDo.buildTable);
} else {
addJobToDo(JobsToDo.locationPatch);
possibleJobs.add(JobsToDo.locationPatch);
}
break;
case CupPartionScanner.SINGLE_COMMENT:
if(event.getText().equals("/")) {
addJobToDo(JobsToDo.parseCode);
addJobToDo(JobsToDo.buildTable);
if (event.getText().equals("/")) {
possibleJobs.add(JobsToDo.parseCode);
possibleJobs.add(JobsToDo.buildTable);
} else {
addJobToDo(JobsToDo.locationPatch);
possibleJobs.add(JobsToDo.locationPatch);
}
break;
default:
addJobToDo(JobsToDo.parseCode);
addJobToDo(JobsToDo.buildTable);
possibleJobs.add(JobsToDo.parseCode);
possibleJobs.add(JobsToDo.buildTable);
break;
}
}
......@@ -210,18 +242,79 @@ public class Controller {
e.printStackTrace();
}
if (myJob.getState() == Job.NONE
|| (!myJob.running() && myJob.getState() == Job.SLEEPING)) {
myJob.cancel();
myJob.revNumber = RevisionManager.increment(document);
myJob.schedule(SET_CHANGED_AFTER_SECONDS * 1000);
} else {
RevisionManager.increment(document);
changeJobReRunRequested = System.currentTimeMillis();
for (JobsToDo job : possibleJobs) {
if (observedJobs.contains(job)) {
addJobToDo(job);
}
}
runJobs(document);
}
}
private void runJobs(IDocument document) {
if (myJob.getState() == Job.NONE
|| (!myJob.running() && myJob.getState() == Job.SLEEPING)) {
myJob.cancel();
myJob.revNumber = RevisionManager.increment(document);
myJob.schedule(SET_CHANGED_AFTER_SECONDS * 1000);
} else {
RevisionManager.increment(document);
changeJobReRunRequested = System.currentTimeMillis();
}
}
/**
* Request a run of all necessary jobs, this method returns true if the jobs
* will run and false if no job is required because the actual model
* revision has the same number as the document revision
*
* @return if the job will ever run
*/
public boolean requestJobRun() {
IDocument document = myEditor.getDocumentProvider().getDocument(
myEditor.getEditorInput());
EnumSet<JobsToDo> observedJobs = EnumSet.noneOf(JobsToDo.class);
synchronized (controllerObservers) {
for (RegisterForControllerChanges observer : controllerObservers) {
observedJobs.addAll(observer.getRequiredJobs());
}
}
for (JobsToDo job : observedJobs) {
switch (job) {
case locationPatch:
addJobToDo(job);
break;
case parseCode:
long modelAstRev = Model.getInstanceForDocument(document)
.getAstModelRevisionNumber();
if (modelAstRev != RevisionManager.get(document)) {
addJobToDo(job);
}
break;
case buildTable:
long modelLaLrRev = Model.getInstanceForDocument(document)
.getLaLrRevisionNumber();
if (modelLaLrRev != RevisionManager.get(document)) {
addJobToDo(job);
}
break;
default:
break;
}
}
synchronized (jobElements) {
if (jobElements.isEmpty()) {
return false;
}
}
runJobs(document);
return true;
}
/*
......@@ -237,8 +330,9 @@ public class Controller {
changeJobReRunRequested = 0;
long remainderOfDelay = (SET_CHANGED_AFTER_SECONDS * 100)
- (System.currentTimeMillis() - changeJobReRunRequested);
myJob.revNumber = RevisionManager.get(myEditor.getDocumentProvider().getDocument(
myEditor.getEditorInput()));
myJob.revNumber = RevisionManager.get(myEditor
.getDocumentProvider().getDocument(
myEditor.getEditorInput()));
myJob.schedule((remainderOfDelay >= 0) ? remainderOfDelay : 0);
}
}
......
......@@ -109,17 +109,9 @@ public class DocumentDidChangeJob extends Job {
}
if (jobs.contains(JobsToDo.locationPatch)) {
// TODO: location Patch verifizieren
Position oldRemovedFrom = null;
int oldRemovedLength = 0;
int newLength = 0;
LocationPatchVisitor visitor = new LocationPatchVisitor(
oldRemovedFrom, oldRemovedLength, newLength);
if (result != null)
result.accept(visitor, null);
else {
System.out.println("ERROR: Can't run LocationPatchVisitor!");
}
LocationPatchJob locPatch = new LocationPatchJob();
locPatch.setSystem(true);
locPatch.schedule();
}
IFile file = ((FileEditorInput) myEditor.getEditorInput()).getFile();
......@@ -287,6 +279,31 @@ public class DocumentDidChangeJob extends Job {
}
class LocationPatchJob extends UIJob {
public LocationPatchJob() {
super("Location Patch Job");
}
@Override
public IStatus runInUIThread(IProgressMonitor monitor) {
// TODO: location Patch verifizieren
Position oldRemovedFrom = null;
int oldRemovedLength = 0;
int newLength = 0;
LocationPatchVisitor visitor = new LocationPatchVisitor(
oldRemovedFrom, oldRemovedLength, newLength);
if (result != null)
result.accept(visitor, null);
else {
System.out.println("ERROR: Can't run LocationPatchVisitor!");
}
return Status.OK_STATUS;
}
}
class SetupJob extends UIJob {
private DocumentDidChangeJob documentDidChangeJob;
......
......@@ -3,6 +3,7 @@ package de.tum.in.www2.cupplugin.editors;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
......@@ -47,6 +48,9 @@ import de.in.tum.www2.cup.CupContext;
import de.in.tum.www2.cup.LALRResult;
import de.in.tum.www2.cup.internal.lalr_state;
import de.in.tum.www2.cup.internal.lalr_transition;
import de.tum.in.www2.cupplugin.controller.Controller;
import de.tum.in.www2.cupplugin.controller.Controller.JobsToDo;
import de.tum.in.www2.cupplugin.controller.RegisterForControllerChanges;
import de.tum.in.www2.cupplugin.editors.CupReduceGraphView.GraphNodeContentProvider;
import de.tum.in.www2.cupplugin.editors.CupReduceGraphView.ModelRebuildJob;
import de.tum.in.www2.cupplugin.editors.CupReduceGraphView.ParserNodeModelContentProvider;
......@@ -57,10 +61,12 @@ import de.tum.in.www2.cupplugin.model.CupParserLaLrChangeObserver;
import de.tum.in.www2.cupplugin.model.Model;
public class CupConflictGraphView implements CupParserLaLrChangeObserver,
IZoomableWorkbenchPart, CupEditorPageVisibility {
IZoomableWorkbenchPart, CupEditorPageVisibility, RegisterForControllerChanges {
private static final int BUTTON_WIDTH = 120;
private CupTextEditor editor;
public GraphViewer graphViewer;
private Model parseModel;
private ParserConflictNodeModelContentProvider nodeModel;
......@@ -80,7 +86,12 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
private Stack<DFSNode> searchStack;
private HashSet<lalr_state> visitedStates;
public CupConflictGraphView(Composite parent, IDocument doc) {
public CupConflictGraphView(Composite parent, CupTextEditor editor) {
this.editor = editor;
Controller.getInstance(editor).registerObserver(this);
IDocument doc = editor.getDocumentProvider().getDocument(
editor.getEditorInput());
this.parseModel = Model.getInstanceForDocument(doc);
this.parseModel.registerModelObserver(this);
this.parentComposite = parent;
......@@ -228,6 +239,9 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
@Override
public void ModelChange(Model model) {
if (!isVisible) {
return;
}
stateHashMap.clear();
CupContext lalrContext = model.getLaLrContext();
lalr_state.lalr_state_shared sharedState = lalr_state
......@@ -312,6 +326,13 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
private boolean DFSSearch(DFSNode state, int searchIndex) {
if (state.getState().index() == searchIndex) {
// TODO: Want to only end with nonTerminals?
/*
if(state.predecessor != null && !state.predTransition.on_symbol().is_non_term()) {
return true;
}
return false;
*/
return true;
}
visitedStates.add(state.getState());
......@@ -356,11 +377,18 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
@Override
public void willBecomeVisible() {
// this.graphViewer.refresh();
isVisible = true;
if(!Controller.getInstance(editor).requestJobRun()) {
// TODO: Handle no rerun!!
IDocument doc = editor.getDocumentProvider().getDocument(
editor.getEditorInput());
ModelChange(Model.getInstanceForDocument(doc));
}
}
@Override
public void becameHidden() {
// TODO Auto-generated method stub
isVisible = false;
}
......@@ -579,25 +607,21 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
@Override
public Color getBorderColor(Object entity) {
// TODO Auto-generated method stub
return null;
}
@Override
public Color getBorderHighlightColor(Object entity) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getBorderWidth(Object entity) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Color getBackgroundColour(Object entity) {
// TODO Auto-generated method stub
if ((showColordEndStates || showColordStartState)
&& entity instanceof ParserConflictNode) {
ParserConflictNode n = (ParserConflictNode) entity;
......@@ -632,7 +656,6 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
@Override
public boolean fisheyeNode(Object entity) {
// TODO Auto-generated method stub
return false;
}
......@@ -643,21 +666,27 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
@Override
public Color getColor(Object src, Object dest) {
// TODO Auto-generated method stub
return null;
}
@Override
public Color getHighlightColor(Object src, Object dest) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getLineWidth(Object src, Object dest) {
// TODO Auto-generated method stub
return 0;
}
}
@Override
public EnumSet<JobsToDo> getRequiredJobs() {
EnumSet<JobsToDo> requestedJobs = EnumSet.noneOf(JobsToDo.class);
if(isVisible) {
requestedJobs = EnumSet.of(JobsToDo.buildTable, JobsToDo.parseCode);
}
return requestedJobs;
}
}
......@@ -232,12 +232,13 @@ public class CupReduceGraphView implements CupParserLaLrChangeObserver,
LayoutAlgorithm layout = new CompositeLayoutAlgorithm(
LayoutStyles.NO_LAYOUT_NODE_RESIZING, new LayoutAlgorithm[] {
new SpringLayoutAlgorithm(
new GridLayoutAlgorithm(
LayoutStyles.NO_LAYOUT_NODE_RESIZING),
new HorizontalShift(
LayoutStyles.NO_LAYOUT_NODE_RESIZING),
new GridLayoutAlgorithm(
LayoutStyles.NO_LAYOUT_NODE_RESIZING) });
LayoutStyles.NO_LAYOUT_NODE_RESIZING)//,
//new GridLayoutAlgorithm(
// LayoutStyles.NO_LAYOUT_NODE_RESIZING)
});
graphViewer.setLayoutAlgorithm(layout, true);
graphViewer.applyLayout();
synchronized (CupReduceGraphView.modelBuildLock) {
......
......@@ -88,7 +88,7 @@ public class MultiPageEditor extends MultiPageEditorPart implements
graphReduceView = new CupReduceGraphView(composite, doc);
int index = addPage(composite);
setPageText(index, "Visualization Reduce Graph");
setPageText(index, "Automaton View (costly)");
}
void createOverviewPage() {
......@@ -122,7 +122,7 @@ public class MultiPageEditor extends MultiPageEditorPart implements
Composite composite = new Composite(getContainer(), SWT.NONE);
IDocument doc = editor.getDocumentProvider().getDocument(
editor.getEditorInput());
conflictGraphView = new CupConflictGraphView(composite, doc);
conflictGraphView = new CupConflictGraphView(composite, editor);
addPage(CONFLICT_GRAPH_PAGE_INDEX, composite);
setPageText(CONFLICT_GRAPH_PAGE_INDEX, "Conflict Graph");
}
......
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