summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjcheuoua2006-11-15 02:13:44 (EST)
committerjcheuoua2006-11-15 02:13:44 (EST)
commit85e924faca7becbb1ce6bc080e8a4c2784d69587 (patch)
treef82873fde98e749a89c7e44cd2b4eb7dc91ac9b1
parentf315bc92f37fa24cfe3cd1b0949c5371076a372d (diff)
downloadorg.eclipse.jet-85e924faca7becbb1ce6bc080e8a4c2784d69587.zip
org.eclipse.jet-85e924faca7becbb1ce6bc080e8a4c2784d69587.tar.gz
org.eclipse.jet-85e924faca7becbb1ce6bc080e8a4c2784d69587.tar.bz2
Outline selection + minor reformating
-rw-r--r--plugins/org.eclipse.jet.editor/src/org/eclipse/jet/editor/JETEditorHelper.java1
-rw-r--r--plugins/org.eclipse.jet.editor/src/org/eclipse/jet/editor/JETTextEditor.java197
-rw-r--r--plugins/org.eclipse.jet.editor/src/org/eclipse/jet/editor/partition/JETDocumentPartitionScanner.java3
3 files changed, 181 insertions, 20 deletions
diff --git a/plugins/org.eclipse.jet.editor/src/org/eclipse/jet/editor/JETEditorHelper.java b/plugins/org.eclipse.jet.editor/src/org/eclipse/jet/editor/JETEditorHelper.java
index 028941c..7701b82 100644
--- a/plugins/org.eclipse.jet.editor/src/org/eclipse/jet/editor/JETEditorHelper.java
+++ b/plugins/org.eclipse.jet.editor/src/org/eclipse/jet/editor/JETEditorHelper.java
@@ -173,6 +173,7 @@ public class JETEditorHelper {
}
}
+
// public static String computeJavaSource(JET2CompilationUnit cu, Map mappedPositions) {
// Map variables = new HashMap();
// variables.put("org.eclipse.jet.storeJavaMappingPositions", mappedPositions);
diff --git a/plugins/org.eclipse.jet.editor/src/org/eclipse/jet/editor/JETTextEditor.java b/plugins/org.eclipse.jet.editor/src/org/eclipse/jet/editor/JETTextEditor.java
index 1695b47..8e1cd3a 100644
--- a/plugins/org.eclipse.jet.editor/src/org/eclipse/jet/editor/JETTextEditor.java
+++ b/plugins/org.eclipse.jet.editor/src/org/eclipse/jet/editor/JETTextEditor.java
@@ -4,6 +4,8 @@ import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
@@ -11,13 +13,26 @@ import org.eclipse.core.resources.IFile;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jet.JET2Platform;
+import org.eclipse.jet.compiler.Comment;
+import org.eclipse.jet.compiler.DefaultJET2ASTVisitor;
+import org.eclipse.jet.compiler.JET2ASTElement;
import org.eclipse.jet.compiler.JET2CompilationUnit;
+import org.eclipse.jet.compiler.JET2Declaration;
+import org.eclipse.jet.compiler.JET2Directive;
+import org.eclipse.jet.compiler.JET2Expression;
+import org.eclipse.jet.compiler.JET2Scriptlet;
+import org.eclipse.jet.compiler.TextElement;
+import org.eclipse.jet.compiler.XMLBodyElement;
+import org.eclipse.jet.compiler.XMLBodyElementEnd;
+import org.eclipse.jet.compiler.XMLEmptyElement;
import org.eclipse.jet.editor.configuration.JETDocumentProvider;
import org.eclipse.jet.editor.configuration.JETEditorPreferenceConstants;
import org.eclipse.jet.editor.configuration.JETSourceViewerConfiguration;
import org.eclipse.jet.editor.configuration.JETTokenStyleManager;
import org.eclipse.jet.editor.outline.JETOutlinePage;
import org.eclipse.jet.editor.partition.JETDocumentPartitionScanner;
+import org.eclipse.jet.internal.parser.JETParser;
+import org.eclipse.jet.internal.parser.JETReader;
import org.eclipse.jet.taglib.TagLibrary;
import org.eclipse.jet.taglib.TagLibraryManager;
import org.eclipse.jet.taglib.TagLibraryReference;
@@ -29,12 +44,16 @@ import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.DefaultInformationControl;
import org.eclipse.jface.text.IInformationControl;
import org.eclipse.jface.text.IInformationControlCreator;
+import org.eclipse.jface.text.ITextListener;
+import org.eclipse.jface.text.Position;
+import org.eclipse.jface.text.TextEvent;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.jface.text.source.projection.ProjectionSupport;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
+import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
@@ -61,6 +80,8 @@ public class JETTextEditor extends TextEditor {
private JET2CompilationUnit cUnit;
private IJavaProject javaProject;
+ private Map positions = Collections.EMPTY_MAP;
+ private Map registeredPrefixMap;
public JETTextEditor() {
super();
@@ -70,12 +91,7 @@ public class JETTextEditor extends TextEditor {
tokenStyleManager = new JETTokenStyleManager(preferenceStore);
partitionScanner = new JETDocumentPartitionScanner(this);
setSourceViewerConfiguration(new JETSourceViewerConfiguration(this));
- setDocumentProvider(new JETDocumentProvider(this) {
- public void changed(Object element) {
- super.changed(element);
- JETTextEditor.this.changed();
- }
- });
+ setDocumentProvider(new JETDocumentProvider(this));
}
public void dispose() {
@@ -111,6 +127,60 @@ public class JETTextEditor extends TextEditor {
}
});
projectionSupport.install();
+ sourceViewer.addTextListener(new ITextListener() {
+ public void textChanged(TextEvent event) {
+ int changeOffset = event.getOffset();
+
+ int replacedTextLength = event.getReplacedText() == null ? 0 : event.getReplacedText().length();
+ int textLength = event.getText() == null ? 0 : event.getText().length();
+
+ int shift = textLength - replacedTextLength; // can be negative
+
+ JET2ASTElement elementAtChangeOffset = getCachedASTElement(changeOffset);
+ JET2ASTElement elementAtEndOfChange = getCachedASTElement(changeOffset + replacedTextLength);
+
+ if (elementAtChangeOffset != null) {
+ int start = elementAtChangeOffset.getStart();
+ int end = elementAtEndOfChange == null ?
+ elementAtChangeOffset.getEnd() :
+ elementAtEndOfChange.getEnd();
+ try {
+ String newText =
+ sourceViewer.getDocument().get(start, end - start + shift);
+ // reparse this portion of the document only
+// JET2CompilationUnit incrementalCUnit = new JET2CompilationUnit();
+// Map cUnitPredefLibs = new HashMap();
+// for (int i = 0; i < cUnit.getTagLibraryReferences().length; i++) {
+// TagLibraryReference tagLibRef = cUnit.getTagLibraryReferences()[i];
+// cUnitPredefLibs.put(tagLibRef.getPrefix(), tagLibRef.getTagLibraryId());
+// }
+// incrementalCUnit.setPredefinedTagLibraries(cUnitPredefLibs);
+// incrementalCUnit.parse(newText);
+// List parsedBodyElements = incrementalCUnit.getBodyElements();
+//
+// // remove previous body elements on the original cUnit
+// int index = cUnit.getBodyElements().indexOf(elementAtChangeOffset);
+// if (elementAtChangeOffset == elementAtEndOfChange || elementAtEndOfChange == null) {
+// cUnit.getBodyElements().remove(elementAtChangeOffset);
+// } else {
+// JET2ASTElement nextElement = elementAtChangeOffset.getNextElement();
+// while (nextElement != elementAtEndOfChange) {
+// cUnit.getBodyElements().remove(elementAtEndOfChange);
+// nextElement = nextElement.getNextElement();
+// }
+// cUnit.getBodyElements().remove(elementAtChangeOffset);
+// cUnit.getBodyElements().remove(elementAtEndOfChange);
+// }
+// cUnit.getBodyElements().addAll(index, parsedBodyElements);
+// if (outlinePage != null)
+// ((JETOutlinePage)outlinePage).setInput();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ //editorTextChanged();
+ }
+ });
return sourceViewer;
}
@@ -132,16 +202,17 @@ public class JETTextEditor extends TextEditor {
protected void handleEditorInputChanged() {
super.handleEditorInputChanged();
- changed();
+ editorTextChanged();
}
- private void changed() {
- if (outlinePage != null && outlinePage.getControl().isVisible())
- ((JETOutlinePage)outlinePage).setInput();
+ private void editorTextChanged() {
+ cUnit = null;
+// if (outlinePage != null && outlinePage.getControl().isVisible())
+// ((JETOutlinePage)outlinePage).setInput();
}
public JET2CompilationUnit requestCompilationUnit() {
- if (cUnit == null || isDirty()) {
+ if (cUnit == null) {
IEditorInput editorInput = getEditorInput();
String charset = "UTF-8";
InputStream is = new ByteArrayInputStream(sourceViewer.getDocument().get().getBytes());
@@ -153,25 +224,45 @@ public class JETTextEditor extends TextEditor {
IJETBundleDescriptor descriptor = bundleManager.getDescriptorForProject(jetTemplateFile.getProject().getName());
predefinedLibraryMap.putAll(descriptor != null ? getPredefinedPrefixMap(descriptor) : Collections.EMPTY_MAP);
}
- cUnit.parse(is, charset);
cUnit.setPredefinedTagLibraries(predefinedLibraryMap);
+ cUnit.parse(is, charset);
+ PositionsCollector collector = new PositionsCollector();
+ cUnit.accept(collector);
+ positions = collector.getPositions();
}
return cUnit;
}
+ public JET2ASTElement getASTElement(int offset) {
+ if (cUnit == null)
+ requestCompilationUnit(); // recompute the positions if the compilation unit has been invalidated
+ return getCachedASTElement(offset);
+ }
+
+ private JET2ASTElement getCachedASTElement(int offset) {
+ for (Iterator iterator = positions.keySet().iterator(); iterator.hasNext();) {
+ Position pos = (Position) iterator.next();
+ if (pos.offset <= offset && (pos.length + pos.offset) > offset)
+ return (JET2ASTElement) positions.get(pos);
+ }
+ return null;
+ }
+
public IJavaProject getJETJavaProject() {
return javaProject;
}
private Map getRegisteredPrefixMap() {
- String[] tagLibIds = TagLibraryManager.getInstance().getKnownLibraryIds();
- final Map result = new HashMap(tagLibIds.length);
- for (int i = 0; i < tagLibIds.length; i++) {
- String id = tagLibIds[i];
- TagLibrary tagLib = TagLibraryManager.getInstance().getTagLibrary(id, true);
- result.put(tagLib.getDefaultPrefix(), tagLib.getLibraryId());
+ if (registeredPrefixMap == null) {
+ String[] tagLibIds = TagLibraryManager.getInstance().getKnownLibraryIds();
+ registeredPrefixMap = new HashMap(tagLibIds.length);
+ for (int i = 0; i < tagLibIds.length; i++) {
+ String id = tagLibIds[i];
+ TagLibrary tagLib = TagLibraryManager.getInstance().getTagLibrary(id, true);
+ registeredPrefixMap.put(tagLib.getDefaultPrefix(), tagLib.getLibraryId());
+ }
}
- return result;
+ return registeredPrefixMap;
}
private Map getPredefinedPrefixMap(IJETBundleDescriptor descriptor) {
@@ -244,11 +335,77 @@ public class JETTextEditor extends TextEditor {
protected void handleContentOutlineSelection(ISelection selection) {
// TODO Auto-generated method stub
-
+ IStructuredSelection ssel = (IStructuredSelection) selection;
+ Object firstElement = ssel.getFirstElement();
+ if (firstElement instanceof JET2ASTElement) {
+ JET2ASTElement astElement = (JET2ASTElement) firstElement;
+ int start = astElement.getStart();
+ int end = astElement.getEnd();
+ if (astElement instanceof TextElement) {
+ if (astElement.getPrevElement()!= null)
+ start = astElement.getPrevElement().getEnd();
+ else
+ start = 0;
+ if (astElement.getNextElement() != null)
+ end = astElement.getNextElement().getStart();
+ else
+ end = sourceViewer.getDocument().getLength();
+ }
+ sourceViewer.setSelectedRange(start, end - start);
+ sourceViewer.revealRange(start, end - start);
+ }
}
public void createContextMenuFor(TreeViewer contentOutlineViewer) {
// TODO Auto-generated method stub
}
+
+ private static class PositionsCollector extends DefaultJET2ASTVisitor {
+ private Map positions = new HashMap();
+
+ public Map getPositions() {
+ return positions;
+ }
+
+ public void visit(JET2Declaration declaration) {
+ matchOffset(declaration);
+ }
+
+ public void visit(JET2Directive directive) {
+ matchOffset(directive);
+ }
+
+ public void visit(JET2Expression expression) {
+ matchOffset(expression);
+ }
+
+ public void visit(JET2Scriptlet scriptlet) {
+ matchOffset(scriptlet);
+ }
+
+ public void visit(TextElement text) {
+ matchOffset(text);
+ }
+
+ public void visit(XMLEmptyElement xmlEmptyElement) {
+ matchOffset(xmlEmptyElement);
+ }
+
+ public void visit(XMLBodyElement xmlBodyElement) {
+ matchOffset(xmlBodyElement);
+ }
+
+ public void visit(XMLBodyElementEnd xmlBodyElementEnd) {
+ matchOffset(xmlBodyElementEnd);
+ }
+
+ public void visit(Comment comment) {
+ matchOffset(comment);
+ }
+
+ public void matchOffset(JET2ASTElement element) {
+ positions.put(new Position(element.getStart(), element.getEnd() - element.getStart()), element);
+ }
+ }
}
diff --git a/plugins/org.eclipse.jet.editor/src/org/eclipse/jet/editor/partition/JETDocumentPartitionScanner.java b/plugins/org.eclipse.jet.editor/src/org/eclipse/jet/editor/partition/JETDocumentPartitionScanner.java
index a1bc135..c53b865 100644
--- a/plugins/org.eclipse.jet.editor/src/org/eclipse/jet/editor/partition/JETDocumentPartitionScanner.java
+++ b/plugins/org.eclipse.jet.editor/src/org/eclipse/jet/editor/partition/JETDocumentPartitionScanner.java
@@ -132,6 +132,9 @@ public class JETDocumentPartitionScanner extends RuleBasedPartitionScanner {
setPredicateRules(result);
}
+ public int getNextCharacterOffset() {
+ return fOffset;
+ }
private void initPartitionScannerDelegetes() {
partitionConfigurationDelegates = new ArrayList();
IExtensionRegistry registry = Platform.getExtensionRegistry();