Commit 361c7558 authored by Sebastian Pretscher's avatar Sebastian Pretscher

Added better job handling, work in progres...

parent 3823a240
package de.tum.in.www2.cupplugin.controller;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITypedRegion;
import de.tum.in.www2.cupplugin.editors.CupPartionScanner;
import de.tum.in.www2.cupplugin.editors.CupTextEditor;
public class Controller {
public static enum JobsToDo {
doNothing, parseCode, locationPatch, buildTable
};
// The Job handling changes in a document is triggered after x seconds
static final int SET_CHANGED_AFTER_SECONDS = 3;
......@@ -26,6 +39,10 @@ public class Controller {
// a lock, since some operations might not be threadsafe
private final Object lock = new Object();
private List<DocumentEvent> documentEvents;
private final EnumSet<JobsToDo> jobElements = EnumSet
.noneOf(JobsToDo.class);
/*
* Static singleton getter
*/
......@@ -38,14 +55,15 @@ public class Controller {
return instances.get(edit);
}
}
public static Controller getInstance(IDocument doc) {
for(Map.Entry<CupTextEditor,Controller> e : instances.entrySet()){
if(e.getKey().getDocumentProvider().getDocument(e.getKey().getEditorInput()).equals(doc)){
for (Map.Entry<CupTextEditor, Controller> e : instances.entrySet()) {
if (e.getKey().getDocumentProvider()
.getDocument(e.getKey().getEditorInput()).equals(doc)) {
return e.getValue();
}
}
//this is hacky, but it shouldn't happen anyways...
// this is hacky, but it shouldn't happen anyways...
return getInstance(AnonymousEditor);
}
......@@ -56,16 +74,99 @@ public class Controller {
myEditor = editor;
myJob = new DocumentDidChangeJob(editor);
myJob.setSystem(true);
documentEvents = new LinkedList<DocumentEvent>();
}
public void addDocumentEvent(DocumentEvent e) {
synchronized (documentEvents) {
this.documentEvents.add(e);
}
}
public List<DocumentEvent> popAllDocumentEvents() {
synchronized (documentEvents) {
List<DocumentEvent> popList = this.documentEvents;
this.documentEvents = new LinkedList<DocumentEvent>();
return popList;
}
}
public void addJobToDo(JobsToDo job) {
synchronized (jobElements) {
jobElements.add(job);
}
}
public EnumSet<JobsToDo> popJobsToDo() {
synchronized (jobElements) {
final EnumSet<JobsToDo> popJobs = EnumSet.noneOf(JobsToDo.class);
popJobs.addAll(jobElements);
jobElements.clear();
return popJobs;
}
}
/*
* notify method that's to be called, when a change in the document happens
*/
public void notifyChange() {
public void notifyChange(DocumentEvent event) {
synchronized (lock) {
addDocumentEvent(event);
IDocument document = myEditor.getDocumentProvider().getDocument(
myEditor.getEditorInput());
try {
ITypedRegion region = document.getPartition(event.getOffset());
if (event.getText().trim().length() == 0) {
if (event.getText().equals(
System.getProperty("line.separator"))) {
// Some debug outut...
System.out.println("new line");
addJobToDo(JobsToDo.locationPatch);
}
if (event.getText().equals("")) {
System.out.println("delete");
char beforeDelete = document.get().charAt(
event.getOffset() - 1);
//System.out.println(beforeDelete);
if (!region.getType().equals(CupPartionScanner.JAVA_CODE)) {
if (beforeDelete == '*' || beforeDelete == '/'
|| beforeDelete == '{') {
System.out.println("comment delete");
addJobToDo(JobsToDo.parseCode);
addJobToDo(JobsToDo.buildTable);
}
}
}
} else {
String regionType = region.getType();
switch (regionType) {
case CupPartionScanner.JAVA_CODE:
addJobToDo(JobsToDo.locationPatch);
break;
case CupPartionScanner.MULTILINE_COMMENT:
addJobToDo(JobsToDo.locationPatch);
break;
case CupPartionScanner.SINGLE_COMMENT:
addJobToDo(JobsToDo.locationPatch);
break;
default:
addJobToDo(JobsToDo.parseCode);
addJobToDo(JobsToDo.buildTable);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (myJob.getState() == Job.NONE
|| (!myJob.running() && myJob.getState() == Job.SLEEPING)) {
myJob.cancel();
myJob.schedule(SET_CHANGED_AFTER_SECONDS * 1000);
} else {
......@@ -79,11 +180,14 @@ public class Controller {
*/
public void notifyChangeJobFinished() {
synchronized (lock) {
System.out.println("job finished -> " + changeJobReRunRequested); // Log message TODO: DELETE!
System.out.println("job finished -> " + changeJobReRunRequested); // Log
// message
// TODO:
// DELETE!
if (changeJobReRunRequested > 0) {
changeJobReRunRequested = 0;
long remainderOfDelay = (SET_CHANGED_AFTER_SECONDS * 1000)
long remainderOfDelay = (SET_CHANGED_AFTER_SECONDS * 100)
- (System.currentTimeMillis() - changeJobReRunRequested);
myJob.schedule((remainderOfDelay >= 0) ? remainderOfDelay : 0);
}
......
package de.tum.in.www2.cupplugin.controller;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.eclipse.core.resources.IMarker;
import java.util.EnumSet;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.ui.progress.UIJob;
import de.in.tum.www2.cup.CupContext;
import de.in.tum.www2.cup.CupParser;
import de.in.tum.www2.cup.DefaultErrorReporter;
import de.in.tum.www2.cup.LALRResult;
import de.in.tum.www2.cup.ast.ParserResult;
import de.in.tum.www2.cup.internal.internal_error;
import de.tum.in.www2.cupplugin.controller.Controller.JobsToDo;
import de.tum.in.www2.cupplugin.editors.CupPartionScanner;
import de.tum.in.www2.cupplugin.editors.CupTextEditor;
public class DocumentDidChangeJob extends Job {
boolean iAmCurrentlyRunning = false;
CupTextEditor myEditor = null;
private List<DocumentEvent> documentEvents;
private EnumSet<JobsToDo> jobs;
public DocumentDidChangeJob(CupTextEditor editor) {
super(""+ editor.hashCode());
super("" + editor.hashCode());
myEditor = editor;
}
public boolean running(){
public boolean running() {
return iAmCurrentlyRunning;
}
@Override
protected IStatus run(IProgressMonitor monitor) {
iAmCurrentlyRunning = true;
......@@ -41,9 +41,18 @@ public class DocumentDidChangeJob extends Job {
* TODO: Job bestücken, vll mit callbacks
*/
String codeText = myEditor.get();
InputStream in = new ByteArrayInputStream(codeText.getBytes());
IDocument document = myEditor.getDocumentProvider().getDocument(
myEditor.getEditorInput());
Controller controller = Controller.getInstance(myEditor);
documentEvents = controller.popAllDocumentEvents();
jobs = controller.popJobsToDo();
System.out.println(jobs);
/*
String codeText = document.get();
InputStream in = new ByteArrayInputStream(codeText.getBytes());
CupParser p = new CupParser(new DefaultErrorReporter(), in);
ParserResult result = null;
CupContext context = null;
......@@ -55,37 +64,38 @@ public class DocumentDidChangeJob extends Job {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(result != null) {
if (result != null) {
System.out.println(result.toString());
}
if (context == null) {
} else {
try {
lalrResult = LALRResult.Compute(context, context.start_production);
lalrResult = LALRResult.Compute(context,
context.start_production);
} catch (internal_error e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(lalrResult.action_table);
System.out.println(lalrResult.reduce_table);
System.out.println(lalrResult.action_table);
System.out.println(lalrResult.reduce_table);
}
//nichts mehr ändern ab hier
*/
// nichts mehr ändern ab hier
iAmCurrentlyRunning = false;
CallbackJob cb = new CallbackJob(myEditor);
cb.setSystem(true);
cb.schedule();
return Status.OK_STATUS;
}
class CallbackJob extends UIJob{
class CallbackJob extends UIJob {
CupTextEditor myEditor = null;
public CallbackJob(CupTextEditor editor) {
super(""+ editor.hashCode());
super("" + editor.hashCode());
myEditor = editor;
}
......@@ -94,7 +104,7 @@ public class DocumentDidChangeJob extends Job {
Controller.getInstance(myEditor).notifyChangeJobFinished();
return Status.OK_STATUS;
}
}
}
......@@ -23,6 +23,7 @@ class CTEDocumentListener implements IDocumentListener {
public void documentChanged(DocumentEvent event) {
// TODO: does not belong here...
/*
try {
// file.findMarker();
......@@ -40,14 +41,14 @@ class CTEDocumentListener implements IDocumentListener {
e.printStackTrace();
}
*/
System.out.println("starting job chain"); // Log message TODO: DELETE!
Controller controller = Controller.getInstance(event.getDocument());
controller.notifyChange();
controller.notifyChange(event);
}
}
\ No newline at end of file
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