X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.document.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Fui%2FCSSSourceViewerConfiguration.java;fp=bundles%2Forg.simantics.document.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Fui%2FCSSSourceViewerConfiguration.java;h=32627049b07334ec62b4b6f37fc4ac8b00d5f00e;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.document.ui/src/org/simantics/document/ui/CSSSourceViewerConfiguration.java b/bundles/org.simantics.document.ui/src/org/simantics/document/ui/CSSSourceViewerConfiguration.java new file mode 100644 index 000000000..32627049b --- /dev/null +++ b/bundles/org.simantics.document.ui/src/org/simantics/document/ui/CSSSourceViewerConfiguration.java @@ -0,0 +1,200 @@ +package org.simantics.document.ui; + +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.jface.text.DefaultInformationControl; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IInformationControl; +import org.eclipse.jface.text.IInformationControlCreator; +import org.eclipse.jface.text.ITextHover; +import org.eclipse.jface.text.TextAttribute; +import org.eclipse.jface.text.contentassist.ContentAssistant; +import org.eclipse.jface.text.contentassist.IContentAssistant; +import org.eclipse.jface.text.presentation.IPresentationReconciler; +import org.eclipse.jface.text.presentation.PresentationReconciler; +import org.eclipse.jface.text.rules.DefaultDamagerRepairer; +import org.eclipse.jface.text.rules.EndOfLineRule; +import org.eclipse.jface.text.rules.IRule; +import org.eclipse.jface.text.rules.ITokenScanner; +import org.eclipse.jface.text.rules.IWordDetector; +import org.eclipse.jface.text.rules.MultiLineRule; +import org.eclipse.jface.text.rules.PatternRule; +import org.eclipse.jface.text.rules.RuleBasedScanner; +import org.eclipse.jface.text.rules.Token; +import org.eclipse.jface.text.rules.WordRule; +import org.eclipse.jface.text.source.DefaultAnnotationHover; +import org.eclipse.jface.text.source.IAnnotationHover; +import org.eclipse.jface.text.source.ISharedTextColors; +import org.eclipse.jface.text.source.ISourceViewer; +import org.eclipse.jface.text.source.SourceViewerConfiguration; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Device; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.editors.text.EditorsUI; +import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants; + +public class CSSSourceViewerConfiguration extends SourceViewerConfiguration { + + public static final char[] CONTENT_ASSIST_AUTO_CHARS = new char[] { '.' }; + Device device; + + ISharedTextColors sharedTextColors; + IPreferenceStore preferenceStore; + + private CSSTextEditorEnvironment editorEnvironment; + + public CSSSourceViewerConfiguration(Device device, + ISharedTextColors sharedTextColors) { + this.device = device; + this.sharedTextColors = sharedTextColors; + this.preferenceStore = EditorsUI.getPreferenceStore(); + + this.editorEnvironment = new CSSTextEditorEnvironment(); + } + + public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) { + return new String[] { + IDocument.DEFAULT_CONTENT_TYPE + }; + } + + @Override + public int getTabWidth(ISourceViewer sourceViewer) { + if (preferenceStore == null) + return super.getTabWidth(sourceViewer); + return preferenceStore.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH); + } + + @Override + public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) { + String[] indentPrefixes = getIndentPrefixesForTab(getTabWidth(sourceViewer)); + if (indentPrefixes == null) + return null; + + int length = indentPrefixes.length; + if (length > 2) { + // Swap first with second last + String first = indentPrefixes[0]; + indentPrefixes[0] = indentPrefixes[length - 2]; + indentPrefixes[length - 2] = first; + } + + return indentPrefixes; + } + + public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) { + PresentationReconciler reconciler = new PresentationReconciler(); + + DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getSclTokenScanner()); + + reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE); + reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE); + + return reconciler; + } + + ITokenScanner getSclTokenScanner() { + RuleBasedScanner scanner = new RuleBasedScanner(); + + Font font = new Font(device, "Courier New", 10, SWT.NORMAL); + Font boldFont = new Font(device, "Courier New", 10, SWT.BOLD); + + Token defaultToken = new Token( + new TextAttribute( + sharedTextColors.getColor(new RGB(0, 0, 0)), + null, + 0, + font + )); + Token string = new Token(new TextAttribute( + sharedTextColors.getColor(new RGB(42, 0, 255)), + null, + 0, + font + )); + Token reserved = new Token( + new TextAttribute( + sharedTextColors.getColor(new RGB(127, 0, 85)), + null, + SWT.BOLD, + boldFont + )); + Token comment = new Token(new TextAttribute( + sharedTextColors.getColor(new RGB(63, 127, 95)), + null, + 0, + font + )); + + WordRule reservedWord = new WordRule(new IWordDetector() { + @Override + public boolean isWordStart(char c) { + return Character.isJavaIdentifierStart(c); + } + + @Override + public boolean isWordPart(char c) { + return Character.isJavaIdentifierPart(c) || c=='.'; + } + }); + + reservedWord.addWord("if", reserved); + reservedWord.addWord("then", reserved); + reservedWord.addWord("else", reserved); + reservedWord.addWord("match", reserved); + reservedWord.addWord("with", reserved); + reservedWord.addWord("data", reserved); + reservedWord.addWord("type", reserved); + reservedWord.addWord("class", reserved); + + IRule[] rules = new IRule[] { + //new MultiLineRule("\"\"\"", "\"\"\"", string), + new PatternRule("\"", "\"", string, '\\', true), + new MultiLineRule("/*", "*/", comment), + new EndOfLineRule("//", comment), + reservedWord + }; + scanner.setRules(rules); + scanner.setDefaultReturnToken(defaultToken); + + return scanner; + } + + @Override + public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) { + return new CSSEditorTextHover(sourceViewer, editorEnvironment); + } + + @Override + public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) { + return new DefaultAnnotationHover(); + } + + @Override + public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) { + ContentAssistant assistant = new ContentAssistant(); + assistant.enableColoredLabels(true); + assistant.setStatusLineVisible(true); + + assistant.enableAutoActivation(true); + assistant.enableAutoInsert(true); + assistant.setContentAssistProcessor(new CSSCompletionAssistProcessor(editorEnvironment), IDocument.DEFAULT_CONTENT_TYPE); + assistant.setInformationControlCreator(CREATOR); + assistant.setRestoreCompletionProposalSize(Activator.getDefault().getDialogSettings()); + return assistant; + } + + private static final IInformationControlCreator CREATOR = new IInformationControlCreator() { + + @Override + public IInformationControl createInformationControl(Shell parent) { + return new DefaultInformationControl(parent); + } + }; + +// @Override +// public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) { +// return new ContentFormatter(); +// } +}