From 1a251571f6ce0253ccd9007daa6d17f3de53be4c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hannu=20Niemist=C3=B6?= Date: Thu, 23 Nov 2017 13:19:27 +0200 Subject: [PATCH] New SCL completion implemementation taken into use refs #7638 Change-Id: I1d6a554b8fbff962997da4877691dac773c031fb --- .../SCLModuleEditorDocumentProvider.java | 28 +++-- .../src/org/simantics/scl/ui/Activator.java | 4 + .../SCLSourceViewerConfigurationNew.java | 29 ++--- .../scl/ui/editor2/OpenDeclaration.java | 5 +- .../scl/ui/editor2/SCLModuleEditor2.java | 5 - .../SCLModuleEditor2DocumentProvider.java | 2 +- .../SCLContentAssistProcessor2.java | 104 ++++++++++++++++++ 7 files changed, 139 insertions(+), 38 deletions(-) create mode 100644 bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/completion/SCLContentAssistProcessor2.java diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/SCLModuleEditorDocumentProvider.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/SCLModuleEditorDocumentProvider.java index 4a63200a2..7a317ee5b 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/SCLModuleEditorDocumentProvider.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/SCLModuleEditorDocumentProvider.java @@ -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 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 result = SCLOsgi.MODULE_REPOSITORY.getModule(moduleName, listener); + + if(result instanceof Failure) { + Failure failure = (Failure)result; + setAnnotations(Arrays.asList(failure.errors)); + } + else { + setAnnotations(Collections.emptyList()); + } + + SCLModuleEditorDocumentProvider.this.sourceViewer.updateModuleSource( + (TextualModuleSource)SCLOsgi.SOURCE_REPOSITORY.getModuleSource(moduleName, null)); + } finally { + context.put("graph", oldGraph); } - else { - setAnnotations(Collections.emptyList()); - } - - SCLModuleEditorDocumentProvider.this.sourceViewer.updateCompletionAssistModuleName(moduleName); } }); } diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/Activator.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/Activator.java index f68f2876e..bccd231c2 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/Activator.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/Activator.java @@ -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 diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor/SCLSourceViewerConfigurationNew.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor/SCLSourceViewerConfigurationNew.java index 88e9f8f1b..ea7f1163f 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor/SCLSourceViewerConfigurationNew.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor/SCLSourceViewerConfigurationNew.java @@ -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); } } diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/OpenDeclaration.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/OpenDeclaration.java index 421328c16..087c5b970 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/OpenDeclaration.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/OpenDeclaration.java @@ -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; } 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 b7ab0f170..0bdd6f15a 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 @@ -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(); 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 cf6bf9a87..aed5ec71b 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 @@ -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 index 000000000..1fba83a7d --- /dev/null +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/completion/SCLContentAssistProcessor2.java @@ -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 completions = Completions.findCompletions(new CompletionRequest(repository, moduleSource, sourceText, offset, 1000)); + ICompletionProposal[] proposals = new ICompletionProposal[completions.size()]; + for(int i=0;i