From: jsimomaa Date: Tue, 19 Feb 2019 09:48:26 +0000 (+0200) Subject: Add outline page for SCLModuleEditor2 X-Git-Tag: v1.43.0~136^2~194 X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=9f207b4d5d34917ca528684bd3268a0cf4eff331 Add outline page for SCLModuleEditor2 gitlab #257 Change-Id: I95b26ee8056b16103e12f32711167257b324eacf --- diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/SCLModuleEditor2.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/SCLModuleEditor2.java index 1b2096eb9..9aa1f0ddd 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/SCLModuleEditor2.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/SCLModuleEditor2.java @@ -26,10 +26,12 @@ import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds; import org.eclipse.ui.texteditor.IUpdate; import org.eclipse.ui.texteditor.SourceViewerDecorationSupport; import org.eclipse.ui.texteditor.TextNavigationAction; +import org.eclipse.ui.views.contentoutline.IContentOutlinePage; import org.simantics.scl.ui.editor.SCLSourceViewerConfigurationNew; import org.simantics.scl.ui.editor.completion.SCLTextEditorEnvironment; import org.simantics.scl.ui.editor2.iterator.DocumentCharacterIterator; import org.simantics.scl.ui.editor2.iterator.JavaWordIterator; +import org.simantics.scl.ui.outline.SCLModuleOutlinePage; import com.ibm.icu.text.BreakIterator; @@ -46,12 +48,15 @@ public class SCLModuleEditor2 extends TextEditor { protected ResourceManager resourceManager; private DefaultCharacterPairMatcher matcher; + private SCLModuleOutlinePage outline; + public SCLModuleEditor2() { super(); resourceManager = new LocalResourceManager(JFaceResources.getResources()); SCLSourceViewerConfigurationNew sourceViewerConfiguration = new SCLSourceViewerConfigurationNew(resourceManager); setDocumentProvider(new SCLModuleEditor2DocumentProvider(sourceViewerConfiguration)); setSourceViewerConfiguration(sourceViewerConfiguration); + outline = new SCLModuleOutlinePage(this); } @Override @@ -136,7 +141,16 @@ public class SCLModuleEditor2 extends TextEditor { public IDocument getDocument() { return getSourceViewer().getDocument(); } - + + @SuppressWarnings("unchecked") + @Override + public T getAdapter(Class adapter) { + if (IContentOutlinePage.class.equals(adapter)) { + return (T) outline; + } + return super.getAdapter(adapter); + } + /** * Text navigation action to navigate to the next sub-word. * diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/outline/SCLModuleOutlinePage.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/outline/SCLModuleOutlinePage.java index d333f235a..80e34c374 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/outline/SCLModuleOutlinePage.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/outline/SCLModuleOutlinePage.java @@ -1,96 +1,144 @@ package org.simantics.scl.ui.outline; -import org.eclipse.jface.viewers.ILabelProvider; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +import org.eclipse.jface.resource.JFaceResources; +import org.eclipse.jface.resource.LocalResourceManager; +import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider; +import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider; import org.eclipse.jface.viewers.ILabelProviderListener; import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.ITreeContentProvider; -import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.jface.viewers.SelectionChangedEvent; +import org.eclipse.jface.viewers.StyledString; +import org.eclipse.jface.viewers.TreeSelection; import org.eclipse.jface.viewers.TreeViewer; -import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Control; -import org.eclipse.swt.widgets.Tree; -import org.eclipse.ui.part.Page; -import org.eclipse.ui.views.contentoutline.IContentOutlinePage; +import org.eclipse.ui.views.contentoutline.ContentOutlinePage; +import org.simantics.scl.compiler.elaboration.modules.SCLValue; +import org.simantics.scl.compiler.environment.filter.AcceptAllNamespaceFilter; +import org.simantics.scl.compiler.errors.Failable; +import org.simantics.scl.compiler.errors.Locations; +import org.simantics.scl.compiler.module.Module; +import org.simantics.scl.compiler.module.repository.UpdateListener; +import org.simantics.scl.compiler.source.ModuleSource; +import org.simantics.scl.compiler.types.TCon; +import org.simantics.scl.osgi.SCLOsgi; +import org.simantics.scl.ui.Activator; import org.simantics.scl.ui.editor2.SCLModuleEditor2; +import org.simantics.scl.ui.editor2.SCLModuleEditorInput; -public class SCLModuleOutlinePage extends Page implements IContentOutlinePage { +public class SCLModuleOutlinePage extends ContentOutlinePage { - private SCLOutlineViewer outlineViewer; + private TreeViewer outlineViewer; private SCLModuleEditor2 moduleEditor; - + private UpdateListener updateListener; + private LocalResourceManager resourceManager; + private Image publicImage; + private Image privateImage; + private Image typeImage; + public SCLModuleOutlinePage(SCLModuleEditor2 moduleEditor) { this.moduleEditor = moduleEditor; - } - - @Override - public void addSelectionChangedListener(ISelectionChangedListener listener) { - if (outlineViewer != null) - outlineViewer.addSelectionChangedListener(listener); - } - - @Override - public ISelection getSelection() { - if (outlineViewer != null) - outlineViewer.getSelection(); - return StructuredSelection.EMPTY; - } - - @Override - public void removeSelectionChangedListener(ISelectionChangedListener listener) { - if (outlineViewer != null) - outlineViewer.removeSelectionChangedListener(listener); - } - - @Override - public void setSelection(ISelection selection) { - if (outlineViewer != null) - outlineViewer.setSelection(selection); + resourceManager = new LocalResourceManager(JFaceResources.getResources()); + publicImage = resourceManager + .createImage(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/public_co.gif")); + privateImage = resourceManager + .createImage(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/private_co.gif")); + typeImage = resourceManager + .createImage(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/typedef_obj.gif")); } @Override public void createControl(Composite parent) { - Tree tree = new Tree(parent, SWT.MULTI); - outlineViewer = new SCLOutlineViewer(tree); - + super.createControl(parent); + outlineViewer = getTreeViewer(); + SCLModuleOutlineProvider provider = new SCLModuleOutlineProvider(); + DelegatingStyledCellLabelProvider labelProvider = new DelegatingStyledCellLabelProvider(provider); outlineViewer.setContentProvider(provider); - outlineViewer.setLabelProvider(provider); - - outlineViewer.setInput(moduleEditor.getEditorInput()); + outlineViewer.setLabelProvider(labelProvider); + outlineViewer.addSelectionChangedListener(this); + + SCLModuleEditorInput input = (SCLModuleEditorInput) moduleEditor.getEditorInput(); + ModuleSource moduleSource = input.getAdapter(ModuleSource.class); + updateListener = new UpdateListener() { + + @Override + public void notifyAboutUpdate() { + parent.getDisplay().asyncExec(() -> { + if (!outlineViewer.getControl().isDisposed()) { + outlineViewer.refresh(); + } + }); + } + }; + Failable module = SCLOsgi.MODULE_REPOSITORY.getModule(moduleSource.getModuleName(), updateListener); + Module result = module.getResult(); + outlineViewer.setInput(result); } @Override - public Control getControl() { - if (outlineViewer != null) - outlineViewer.getControl(); - return null; + public void selectionChanged(SelectionChangedEvent event) { + super.selectionChanged(event); + ISelection selection = event.getSelection(); + TreeSelection tselection = (TreeSelection) selection; + if (tselection.getFirstElement() instanceof SCLValue) { + SCLValue value = (SCLValue) tselection.getFirstElement(); + long location = value.definitionLocation; + int begin = Locations.beginOf(location); + int end = Locations.endOf(location); + moduleEditor.selectAndReveal(begin, end - begin); + } else { +// TCon type = (TCon) tselection.getFirstElement(); +// type.loction; // this is missing? + } } @Override - public void setFocus() { - if (outlineViewer != null) - outlineViewer.getControl().setFocus(); + public void dispose() { + resourceManager.dispose(); + super.dispose(); } - protected class SCLOutlineViewer extends TreeViewer { - - public SCLOutlineViewer(Tree tree) { - super(tree); - setAutoExpandLevel(ALL_LEVELS); - setUseHashlookup(true); - } - - - } - - protected class SCLModuleOutlineProvider implements ITreeContentProvider, ILabelProvider { + protected class SCLModuleOutlineProvider implements ITreeContentProvider, IStyledLabelProvider { @Override public Object[] getElements(Object inputElement) { - return null; + Module result = (Module) inputElement; + List values = new ArrayList<>(); + result.findValuesForPrefix("", AcceptAllNamespaceFilter.INSTANCE, value -> { + values.add(value); + }); + values.sort(new Comparator() { + + @Override + public int compare(SCLValue o1, SCLValue o2) { + int isPrivate = Boolean.compare(o1.isPrivate(), o2.isPrivate()); + if (isPrivate != 0) + return isPrivate; + else + return o1.getName().name.compareTo(o2.getName().name); + } + }); + List types = new ArrayList<>(); + result.findTypesForPrefix("", AcceptAllNamespaceFilter.INSTANCE, type -> { + types.add(type); + }); + types.sort(new Comparator() { + + @Override + public int compare(TCon o1, TCon o2) { + return o1.name.compareTo(o2.name); + } + }); + List results = new ArrayList<>(); + results.addAll(types); + results.addAll(values); + return results.toArray(); } @Override @@ -110,7 +158,7 @@ public class SCLModuleOutlinePage extends Page implements IContentOutlinePage { @Override public void addListener(ILabelProviderListener listener) { - + } @Override @@ -120,23 +168,41 @@ public class SCLModuleOutlinePage extends Page implements IContentOutlinePage { @Override public void removeListener(ILabelProviderListener listener) { - + } @Override public Image getImage(Object element) { - return null; + if (element instanceof SCLValue) { + SCLValue value = (SCLValue) element; + if (value.isPrivate()) { + return privateImage; + } else { + return publicImage; + } + } else if (element instanceof TCon) { + return typeImage; + } else { + return null; + } } - @Override - public String getText(Object element) { - return null; - } - @Override public void dispose() { ITreeContentProvider.super.dispose(); } + @Override + public StyledString getStyledText(Object element) { + if (element instanceof SCLValue) { + SCLValue value = (SCLValue) element; + StyledString ss = new StyledString(value.getName().name); + ss.append(" :: " + value.getType().toString(), StyledString.COUNTER_STYLER); + return ss; + } else { + TCon type = (TCon) element; + return new StyledString(type.name); + } + } } }