]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor/SCLSourceViewerConfigurationNew.java
LabelDecorator.decorateLabel can return null
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / editor / SCLSourceViewerConfigurationNew.java
1 package org.simantics.scl.ui.editor;
2
3 import org.eclipse.jface.preference.IPreferenceStore;
4 import org.eclipse.jface.resource.ResourceManager;
5 import org.eclipse.jface.text.DefaultInformationControl;
6 import org.eclipse.jface.text.DefaultTextHover;
7 import org.eclipse.jface.text.IDocument;
8 import org.eclipse.jface.text.IInformationControl;
9 import org.eclipse.jface.text.IInformationControlCreator;
10 import org.eclipse.jface.text.ITextHover;
11 import org.eclipse.jface.text.contentassist.ContentAssistant;
12 import org.eclipse.jface.text.contentassist.IContentAssistant;
13 import org.eclipse.jface.text.presentation.IPresentationReconciler;
14 import org.eclipse.jface.text.source.DefaultAnnotationHover;
15 import org.eclipse.jface.text.source.IAnnotationHover;
16 import org.eclipse.jface.text.source.ISourceViewer;
17 import org.eclipse.jface.text.source.SourceViewerConfiguration;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.ui.editors.text.EditorsUI;
20 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
21 import org.simantics.scl.ui.Activator;
22 import org.simantics.scl.ui.editor.completion.SCLCompletionAssistProcessor;
23 import org.simantics.scl.ui.editor.completion.SCLTextEditorEnvironment;
24 import org.simantics.scl.ui.editor2.SCLPresentationReconciler;
25
26 public class SCLSourceViewerConfigurationNew extends SourceViewerConfiguration {
27
28     public static final char[] CONTENT_ASSIST_AUTO_CHARS = new char[] { '.' };
29     
30     ResourceManager resourceManager;
31     IPreferenceStore preferenceStore;
32     
33     private SCLTextEditorEnvironment sclTextEditorEnvironment;
34     
35     public SCLSourceViewerConfigurationNew(ResourceManager resourceManager) {
36         this.resourceManager = resourceManager;
37         this.preferenceStore = EditorsUI.getPreferenceStore();
38         
39         this.sclTextEditorEnvironment = new SCLTextEditorEnvironment("");
40     }
41
42     public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
43         return new String[] {
44             IDocument.DEFAULT_CONTENT_TYPE
45         };
46     }
47     
48         @Override
49     public int getTabWidth(ISourceViewer sourceViewer) {
50                 if (preferenceStore == null)
51                         return super.getTabWidth(sourceViewer);
52                 return preferenceStore.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH);
53         }
54
55         @Override
56         public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
57                 String[] indentPrefixes = getIndentPrefixesForTab(getTabWidth(sourceViewer));
58                 if (indentPrefixes == null)
59                         return null;
60
61                 int length = indentPrefixes.length;
62                 if (length > 2) {
63                         // Swap first with second last
64                         String first = indentPrefixes[0];
65                         indentPrefixes[0] = indentPrefixes[length - 2];
66                         indentPrefixes[length - 2] = first;
67                 }
68
69                 return indentPrefixes;
70         }
71     
72     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
73         return new SCLPresentationReconciler(resourceManager);
74     }
75     
76     @Override
77     public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
78         //return new SCLEditorTextHover(sourceViewer, sclTextEditorEnvironment);
79         return new DefaultTextHover(sourceViewer);
80     }
81     
82     @Override
83     public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
84         return new DefaultAnnotationHover();
85     }
86     
87     @Override
88     public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
89         ContentAssistant assistant = new ContentAssistant();
90         assistant.enableColoredLabels(true);
91         assistant.setStatusLineVisible(true);
92
93         assistant.enableAutoActivation(true);
94         assistant.enableAutoInsert(true);
95         assistant.setContentAssistProcessor(new SCLCompletionAssistProcessor(sclTextEditorEnvironment), IDocument.DEFAULT_CONTENT_TYPE);
96         assistant.setInformationControlCreator(CREATOR);
97         assistant.setRestoreCompletionProposalSize(Activator.getInstance().getDialogSettings()); 
98         return assistant;
99     }
100     
101     private static final IInformationControlCreator CREATOR = new IInformationControlCreator() {
102         
103         @Override
104         public IInformationControl createInformationControl(Shell parent) {
105             return new DefaultInformationControl(parent);
106         }
107     };
108     
109     public void updateCompletionAssistModuleName(String moduleName) {
110         sclTextEditorEnvironment.updateModuleName(moduleName);
111     }
112     
113 //    @Override
114 //    public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
115 //        return new ContentFormatter();
116 //    }
117     
118     public SCLTextEditorEnvironment getSclTextEditorEnvironment() {
119         return sclTextEditorEnvironment;
120     }
121 }