]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
New SCL completion implemementation taken into use change/1272/4
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Thu, 23 Nov 2017 11:19:27 +0000 (13:19 +0200)
committerHannu Niemistö <hannu.niemisto@semantum.fi>
Sat, 28 Apr 2018 14:31:17 +0000 (17:31 +0300)
refs #7638

Change-Id: I1d6a554b8fbff962997da4877691dac773c031fb

bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/SCLModuleEditorDocumentProvider.java
bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/Activator.java
bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor/SCLSourceViewerConfigurationNew.java
bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/OpenDeclaration.java
bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/SCLModuleEditor2.java
bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/SCLModuleEditor2DocumentProvider.java
bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/completion/SCLContentAssistProcessor2.java [new file with mode: 0644]

index 4a63200a2fb4e231462dc7cf227d3ecd105a99f0..7a317ee5b7ae73d19398137c0ecc999f41b4efea 100644 (file)
@@ -33,6 +33,7 @@ import org.simantics.scl.compiler.errors.Failure;
 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.TextualModuleSource;
 import org.simantics.scl.osgi.SCLOsgi;
 import org.simantics.scl.runtime.SCLContext;
 import org.simantics.scl.ui.editor.SCLSourceViewerConfigurationNew;
@@ -94,18 +95,23 @@ public class SCLModuleEditorDocumentProvider extends AbstractDocumentProvider {
                     return;
                 String moduleName = graph.getURI(resource);
                 SCLContext context = SCLContext.getCurrent();
-                context.put("graph", graph);
-                Failable<Module> result = SCLOsgi.MODULE_REPOSITORY.getModule(moduleName, listener);
-                
-                if(result instanceof Failure) {
-                    Failure failure = (Failure)result;
-                    setAnnotations(Arrays.asList(failure.errors));
+                Object oldGraph = context.put("graph", graph);
+                try {
+                    Failable<Module> result = SCLOsgi.MODULE_REPOSITORY.getModule(moduleName, listener);
+
+                    if(result instanceof Failure) {
+                        Failure failure = (Failure)result;
+                        setAnnotations(Arrays.asList(failure.errors));
+                    }
+                    else {
+                        setAnnotations(Collections.<CompilationError>emptyList());
+                    }
+
+                    SCLModuleEditorDocumentProvider.this.sourceViewer.updateModuleSource(
+                            (TextualModuleSource)SCLOsgi.SOURCE_REPOSITORY.getModuleSource(moduleName, null));
+                } finally {
+                    context.put("graph", oldGraph);
                 }
-                else {
-                    setAnnotations(Collections.<CompilationError>emptyList());
-                }
-                
-                SCLModuleEditorDocumentProvider.this.sourceViewer.updateCompletionAssistModuleName(moduleName);
             }
         });
     }
index f68f2876e83087c3a88dc30a5a456a7028909694..bccd231c2f13e2daf06bff025e7abaddea9aae85 100644 (file)
@@ -37,6 +37,10 @@ public class Activator extends AbstractUIPlugin {
         reg.put("error", Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/error.png") );
         reg.put("import_error", Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/import_error.png") );
         reg.put("warning", Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/warning.png") );
+        
+        reg.put("typedef_obj", Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/typedef_obj.gif") );
+        reg.put("public_co", Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/public_co.gif") );
+        reg.put("container_obj", Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/container_obj.gif") );
     }
     
     @Override
index 88e9f8f1bca88d670967081cc4388372d5da2e05..ea7f1163f7101c25ae3e955e039ec53e3ee27d3f 100644 (file)
@@ -18,10 +18,11 @@ import org.eclipse.jface.text.source.SourceViewerConfiguration;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.ui.editors.text.EditorsUI;
 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
+import org.simantics.scl.compiler.source.TextualModuleSource;
+import org.simantics.scl.osgi.SCLOsgi;
 import org.simantics.scl.ui.Activator;
-import org.simantics.scl.ui.editor.completion.SCLCompletionAssistProcessor;
-import org.simantics.scl.ui.editor.completion.SCLTextEditorEnvironment;
 import org.simantics.scl.ui.editor2.SCLPresentationReconciler;
+import org.simantics.scl.ui.editor2.completion.SCLContentAssistProcessor2;
 
 public class SCLSourceViewerConfigurationNew extends SourceViewerConfiguration {
 
@@ -29,14 +30,12 @@ public class SCLSourceViewerConfigurationNew extends SourceViewerConfiguration {
     
     ResourceManager resourceManager;
     IPreferenceStore preferenceStore;
-    
-    private SCLTextEditorEnvironment sclTextEditorEnvironment;
+    SCLContentAssistProcessor2 contentAssist;
     
     public SCLSourceViewerConfigurationNew(ResourceManager resourceManager) {
         this.resourceManager = resourceManager;
         this.preferenceStore = EditorsUI.getPreferenceStore();
-        
-        this.sclTextEditorEnvironment = new SCLTextEditorEnvironment("");
+        this.contentAssist = new SCLContentAssistProcessor2(SCLOsgi.MODULE_REPOSITORY, resourceManager);
     }
 
     public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
@@ -92,7 +91,8 @@ public class SCLSourceViewerConfigurationNew extends SourceViewerConfiguration {
 
         assistant.enableAutoActivation(true);
         assistant.enableAutoInsert(true);
-        assistant.setContentAssistProcessor(new SCLCompletionAssistProcessor(sclTextEditorEnvironment), IDocument.DEFAULT_CONTENT_TYPE);
+        //assistant.setContentAssistProcessor(new SCLCompletionAssistProcessor(sclTextEditorEnvironment), IDocument.DEFAULT_CONTENT_TYPE);
+        assistant.setContentAssistProcessor(contentAssist, IDocument.DEFAULT_CONTENT_TYPE);
         assistant.setInformationControlCreator(CREATOR);
         assistant.setRestoreCompletionProposalSize(Activator.getInstance().getDialogSettings()); 
         return assistant;
@@ -105,17 +105,8 @@ public class SCLSourceViewerConfigurationNew extends SourceViewerConfiguration {
             return new DefaultInformationControl(parent);
         }
     };
-    
-    public void updateCompletionAssistModuleName(String moduleName) {
-        sclTextEditorEnvironment.updateModuleName(moduleName);
-    }
-    
-//    @Override
-//    public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
-//        return new ContentFormatter();
-//    }
-    
-    public SCLTextEditorEnvironment getSclTextEditorEnvironment() {
-        return sclTextEditorEnvironment;
+
+    public void updateModuleSource(TextualModuleSource moduleSource) {
+        contentAssist.setModuleSource(moduleSource);
     }
 }
index 421328c164222346da2c9eb073f222afe841c601..087c5b970eefa680e220da37eec01c267da9dc2b 100644 (file)
@@ -102,7 +102,7 @@ public class OpenDeclaration extends AbstractHandler {
                 LOGGER.error("Could not open declaration {} {}", input.getModuleName(), lineAtCaret, e);
             }
         }
-        else {
+        /*else {
             // Try to find an identifier at caret
             String identifierAtCaret = extractIdentifierOrSymbolAt(text, caretOffset);
             if(identifierAtCaret.isEmpty())
@@ -113,7 +113,8 @@ public class OpenDeclaration extends AbstractHandler {
             //System.out.println("identifierAtCaret = " + identifierAtCaret + " [" + Locations.beginOf(value.definitionLocation) + ", " + Locations.endOf(value.definitionLocation) + "]");
             if(value != null)
                 OpenSCLDefinition.scheduleOpenDefinition(value.getName().module, value.definitionLocation);
-        }
+                //OpenSCLDefinition.openDefinition(value);
+        }*/
         return null;
     }
 
index b7ab0f170ab92ae8c6572733c04a7d0c956e14d9..0bdd6f15afd6cb70d176a163385ce38721c10ccd 100644 (file)
@@ -127,11 +127,6 @@ public class SCLModuleEditor2 extends TextEditor {
     public boolean isDisposed() {
         return disposed;
     }
-    
-    public SCLTextEditorEnvironment getSCLTextEditorEnvironment() {
-        return ((SCLSourceViewerConfigurationNew)getSourceViewerConfiguration())
-                .getSclTextEditorEnvironment();
-    }
 
     public IDocument getDocument() {
         return getSourceViewer().getDocument();
index cf6bf9a87c6ade46606a2794453891406d19526e..aed5ec71ba8c21cd508c9c2d977ceb2d68244643 100644 (file)
@@ -63,7 +63,7 @@ public class SCLModuleEditor2DocumentProvider extends AbstractDocumentProvider {
         IDocumentPartitioner partitioner = new FastPartitioner(new SCLPartitionScanner(), SCLPartitionScanner.PARTITION_TYPES);
         partitioner.connect(document);
         document.setDocumentPartitioner(partitioner);
-        sourceViewer.updateCompletionAssistModuleName(currentSource.getModuleName());
+        sourceViewer.updateModuleSource(currentSource);
         return document;
     }
     
diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/completion/SCLContentAssistProcessor2.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/completion/SCLContentAssistProcessor2.java
new file mode 100644 (file)
index 0000000..1fba83a
--- /dev/null
@@ -0,0 +1,104 @@
+package org.simantics.scl.ui.editor2.completion;
+
+import java.util.List;
+
+import org.eclipse.jface.resource.ImageRegistry;
+import org.eclipse.jface.resource.ResourceManager;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.contentassist.CompletionProposal;
+import org.eclipse.jface.text.contentassist.ICompletionProposal;
+import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
+import org.eclipse.jface.text.contentassist.IContextInformation;
+import org.eclipse.jface.text.contentassist.IContextInformationValidator;
+import org.eclipse.swt.graphics.Image;
+import org.simantics.scl.compiler.completions.Completion;
+import org.simantics.scl.compiler.completions.CompletionRequest;
+import org.simantics.scl.compiler.completions.CompletionType;
+import org.simantics.scl.compiler.completions.Completions;
+import org.simantics.scl.compiler.module.repository.ModuleRepository;
+import org.simantics.scl.compiler.source.TextualModuleSource;
+import org.simantics.scl.ui.Activator;
+
+public class SCLContentAssistProcessor2 implements IContentAssistProcessor {
+    private final ModuleRepository repository;
+    private final ResourceManager resourceManager;
+    private TextualModuleSource moduleSource;
+    
+    private final Image valueImage;
+    private final Image typeImage;
+    private final Image namespaceImage;
+    
+    public SCLContentAssistProcessor2(ModuleRepository repository, ResourceManager resourceManager) {
+        this.repository = repository;
+        this.resourceManager = resourceManager;
+        
+        ImageRegistry imageRegistry = Activator.getInstance().getImageRegistry();
+        valueImage = resourceManager.createImage(imageRegistry.getDescriptor("public_co"));
+        typeImage = resourceManager.createImage(imageRegistry.getDescriptor("typedef_obj"));
+        namespaceImage = resourceManager.createImage(imageRegistry.getDescriptor("container_obj"));
+    }
+
+    @Override
+    public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
+        String sourceText = viewer.getDocument().get();
+        List<Completion> completions = Completions.findCompletions(new CompletionRequest(repository, moduleSource, sourceText, offset, 1000));
+        ICompletionProposal[] proposals = new ICompletionProposal[completions.size()];
+        for(int i=0;i<completions.size();++i)
+            proposals[i] = convertToProposal(completions.get(i));
+        return proposals;
+    }
+    
+    private Image imageByType(CompletionType completionType) {
+        switch(completionType) {
+        case Value:
+            return valueImage;
+        case Type:
+            return typeImage;
+        case Namespace:
+            return namespaceImage;
+        default:
+            return null;
+        }
+    }
+
+    private ICompletionProposal convertToProposal(Completion completion) {
+        return new CompletionProposal(completion.replacement,
+                completion.startOfReplacedText,
+                completion.lengthOfReplacedText,
+                completion.cursorPositionAfterReplacement-completion.startOfReplacedText,
+                imageByType(completion.completionType),
+                completion.getLabel(),
+                null,
+                completion.documentation);
+    }
+
+    @Override
+    public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
+        return null;
+    }
+
+    @Override
+    public char[] getCompletionProposalAutoActivationCharacters() {
+        return null;
+    }
+
+    @Override
+    public char[] getContextInformationAutoActivationCharacters() {
+        return null;
+    }
+
+    @Override
+    public String getErrorMessage() {
+        return null;
+    }
+
+    @Override
+    public IContextInformationValidator getContextInformationValidator() {
+        return null;
+    }
+
+    public void setModuleSource(TextualModuleSource moduleSource) {
+        this.moduleSource = moduleSource;
+    }
+
+}