From: jsimomaa Date: Sun, 23 Jul 2017 15:39:38 +0000 (+0300) Subject: Preventing unnecessary ModuleSource compilation in SCL-editor X-Git-Tag: v1.31.0~264^2~35 X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=22fc89ea8f464540eab16db50e271b89c16c2842 Preventing unnecessary ModuleSource compilation in SCL-editor Current toTextualModuleSource() is called quite extensively when isModifiable() is called. The module source can be cached as the input of the editor does not tend to change refs #7369 Change-Id: I671af197750301f8bffbb9624f58c5bab656e08b --- diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/SCLModuleEditor2DocumentProvider.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/SCLModuleEditor2DocumentProvider.java index 4dfa0507b..cf6bf9a87 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/SCLModuleEditor2DocumentProvider.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/SCLModuleEditor2DocumentProvider.java @@ -23,32 +23,38 @@ public class SCLModuleEditor2DocumentProvider extends AbstractDocumentProvider { private SCLSourceViewerConfigurationNew sourceViewer; protected AnnotationModel annotationModel = new AnnotationModel(); - public SCLModuleEditor2DocumentProvider( - SCLSourceViewerConfigurationNew sourceViewer) { + private Object currentElement; + private TextualModuleSource currentSource; + + public SCLModuleEditor2DocumentProvider(SCLSourceViewerConfigurationNew sourceViewer) { this.sourceViewer = sourceViewer; } - private static TextualModuleSource toTextualModuleSource(Object input) { - if(!(input instanceof SCLModuleEditorInput)) - return null; - ModuleSource source = ((SCLModuleEditorInput)input).getAdapter(ModuleSource.class); + private void updateTextualModuleSource(Object input) { + if (currentElement != null && currentElement.equals(input)) + return; + currentElement = input; + if(!(currentElement instanceof SCLModuleEditorInput)) + return; + + ModuleSource source = ((SCLModuleEditorInput)currentElement).getAdapter(ModuleSource.class); if(source == null) - return null; + return; if(!(source instanceof TextualModuleSource)) - return null; - return (TextualModuleSource)source; + return; + currentSource = (TextualModuleSource)source; } @Override protected IDocument createDocument(Object element) throws CoreException { - TextualModuleSource source = toTextualModuleSource(element); - if(source == null) + updateTextualModuleSource(element); + if(currentSource == null) throw new CoreException( new Status(Status.ERROR, "org.simantics.scl.ui", "Source for the SCL module could not be found.") ); Document document; try { - document = new Document(source.getSourceText(null)); + document = new Document(currentSource.getSourceText(null)); } catch (IOException e) { throw new CoreException( new Status(Status.ERROR, "org.simantics.scl.ui", "Reading SCL module failed.", e) @@ -57,16 +63,25 @@ public class SCLModuleEditor2DocumentProvider extends AbstractDocumentProvider { IDocumentPartitioner partitioner = new FastPartitioner(new SCLPartitionScanner(), SCLPartitionScanner.PARTITION_TYPES); partitioner.connect(document); document.setDocumentPartitioner(partitioner); - sourceViewer.updateCompletionAssistModuleName(source.getModuleName()); + sourceViewer.updateCompletionAssistModuleName(currentSource.getModuleName()); return document; } + @Override + public void changed(Object element) { + updateTextualModuleSource(element); + } + + @Override + public void aboutToChange(Object element) { + super.aboutToChange(element); + } + @Override public boolean isModifiable(Object element) { - TextualModuleSource source = toTextualModuleSource(element); - if(source == null) + if(currentSource == null) return false; - return source.isUpdateable(); + return currentSource.isUpdateable(); } @Override @@ -84,10 +99,9 @@ public class SCLModuleEditor2DocumentProvider extends AbstractDocumentProvider { @Override protected void doSaveDocument(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite) throws CoreException { - TextualModuleSource source = toTextualModuleSource(element); - if(source == null) + if(currentSource == null) return; - source.update(document.get()); + currentSource.update(document.get()); } @Override