forked from scala-ide/scala-ide
/
ScalaSourceViewerConfiguration.scala
160 lines (139 loc) · 8.68 KB
/
ScalaSourceViewerConfiguration.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright 2005-2010 LAMP/EPFL
*/
// $Id$
package scala.tools.eclipse;
import org.eclipse.jface.text.formatter.MultiPassContentFormatter
import org.eclipse.jface.util.PropertyChangeEvent
import scala.tools.eclipse.semicolon.InferredSemicolonPainter
import org.eclipse.jface.text.ITextViewerExtension2
import org.eclipse.jdt.core.{ IJavaProject, IJavaElement, ICodeAssist }
import org.eclipse.jdt.internal.ui.JavaPlugin
import org.eclipse.jdt.internal.ui.javaeditor.{ IClassFileEditorInput, ICompilationUnitDocumentProvider, JavaElementHyperlinkDetector }
import org.eclipse.jdt.internal.ui.text.ContentAssistPreference
import org.eclipse.jdt.internal.ui.text.java.{ JavaAutoIndentStrategy, JavaStringAutoIndentStrategy, SmartSemicolonAutoEditStrategy }
import org.eclipse.jdt.internal.ui.text.java.hover.{ AbstractJavaEditorTextHover, BestMatchHover }
import org.eclipse.jdt.internal.ui.text.javadoc.JavaDocAutoIndentStrategy
import org.eclipse.jdt.ui.text.{ JavaSourceViewerConfiguration, IJavaPartitions }
import org.eclipse.jface.preference.IPreferenceStore
import org.eclipse.jface.text.{ IAutoEditStrategy, IDocument, ITextHover }
import org.eclipse.jface.text.formatter.ContentFormatter
import org.eclipse.jface.text.contentassist.ContentAssistant
import org.eclipse.jface.text.contentassist.IContentAssistant
import org.eclipse.jface.text.hyperlink.IHyperlinkDetector
import org.eclipse.jface.text.presentation.PresentationReconciler
import org.eclipse.jface.text.rules.{ DefaultDamagerRepairer, RuleBasedScanner, ITokenScanner }
import org.eclipse.jface.text.source.ISourceViewer
import org.eclipse.jface.util.PropertyChangeEvent
import org.eclipse.ui.texteditor.{ HyperlinkDetectorDescriptor, ITextEditor }
import org.eclipse.swt.SWT
import scala.tools.eclipse.ui.{ JdtPreferenceProvider, ScalaAutoIndentStrategy, ScalaIndenter }
import scala.tools.eclipse.util.ReflectionUtils
import scala.tools.eclipse.lexical._
import scala.tools.eclipse.formatter.ScalaFormattingStrategy
import scala.tools.eclipse.ui.AutoCloseBracketStrategy
import scala.tools.eclipse.properties.syntaxcolouring.ScalaSyntaxClasses
import scala.tools.eclipse.hyperlink.text.detector.HyperlinksDetector
import scalariform.ScalaVersions
class ScalaSourceViewerConfiguration(store: IPreferenceStore, scalaPreferenceStore: IPreferenceStore, editor: ITextEditor)
extends JavaSourceViewerConfiguration(JavaPlugin.getDefault.getJavaTextTools.getColorManager, store, editor, IJavaPartitions.JAVA_PARTITIONING) {
private val codeScanner = new ScalaCodeScanner(getColorManager, store, ScalaVersions.DEFAULT)
override def getPresentationReconciler(sv: ISourceViewer) = {
val reconciler = super.getPresentationReconciler(sv).asInstanceOf[PresentationReconciler]
val dr = new ScalaDamagerRepairer(codeScanner)
reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE)
reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE)
def handlePartition(partitionType: String, tokenScanner: ITokenScanner) {
val dr = new DefaultDamagerRepairer(tokenScanner)
reconciler.setDamager(dr, partitionType)
reconciler.setRepairer(dr, partitionType)
}
handlePartition(IDocument.DEFAULT_CONTENT_TYPE, scalaCodeScanner)
handlePartition(IJavaPartitions.JAVA_DOC, scaladocScanner)
handlePartition(IJavaPartitions.JAVA_SINGLE_LINE_COMMENT, singleLineCommentScanner)
handlePartition(IJavaPartitions.JAVA_MULTI_LINE_COMMENT, multiLineCommentScanner)
handlePartition(IJavaPartitions.JAVA_STRING, stringScanner)
handlePartition(ScalaPartitions.SCALA_MULTI_LINE_STRING, multiLineStringScanner)
handlePartition(ScalaPartitions.XML_TAG, xmlTagScanner)
handlePartition(ScalaPartitions.XML_COMMENT, xmlCommentScanner)
handlePartition(ScalaPartitions.XML_CDATA, xmlCDATAScanner)
handlePartition(ScalaPartitions.XML_PCDATA, xmlPCDATAScanner)
handlePartition(ScalaPartitions.XML_PI, xmlPIScanner)
reconciler
}
private val scalaCodeScanner = new ScalaCodeScanner(getColorManager, scalaPreferenceStore, ScalaVersions.DEFAULT)
private val singleLineCommentScanner = new SingleTokenScanner(ScalaSyntaxClasses.SINGLE_LINE_COMMENT, getColorManager, scalaPreferenceStore)
private val multiLineCommentScanner = new SingleTokenScanner(ScalaSyntaxClasses.MULTI_LINE_COMMENT, getColorManager, scalaPreferenceStore)
private val scaladocScanner = new SingleTokenScanner(ScalaSyntaxClasses.SCALADOC, getColorManager, scalaPreferenceStore)
private val stringScanner = new SingleTokenScanner(ScalaSyntaxClasses.STRING, getColorManager, scalaPreferenceStore)
private val multiLineStringScanner = new SingleTokenScanner(ScalaSyntaxClasses.MULTI_LINE_STRING, getColorManager, scalaPreferenceStore)
private val xmlTagScanner = new XmlTagScanner(getColorManager, scalaPreferenceStore)
private val xmlCommentScanner = new XmlCommentScanner(getColorManager, scalaPreferenceStore)
private val xmlCDATAScanner = new XmlCDATAScanner(getColorManager, scalaPreferenceStore)
private val xmlPCDATAScanner = new SingleTokenScanner(ScalaSyntaxClasses.DEFAULT, getColorManager, scalaPreferenceStore)
private val xmlPIScanner = new XmlPIScanner(getColorManager, scalaPreferenceStore)
override def getTextHover(sv: ISourceViewer, contentType: String, stateMask: Int) = {
new ScalaHover(getCodeAssist _)
}
override def getHyperlinkDetectors(sv: ISourceViewer): Array[IHyperlinkDetector] = {
val detector = HyperlinksDetector()
if (editor != null) detector.setContext(editor)
Array(detector)
}
def getCodeAssist: Option[ICodeAssist] = Option(editor) map { editor =>
val input = editor.getEditorInput
val provider = editor.getDocumentProvider
(provider, input) match {
case (icudp: ICompilationUnitDocumentProvider, _) => icudp getWorkingCopy input
case (_, icfei: IClassFileEditorInput) => icfei.getClassFile
case _ => null
}
}
def getProject: IJavaProject = {
getCodeAssist map (_.asInstanceOf[IJavaElement].getJavaProject) orNull
}
/**
* Replica of JavaSourceViewerConfiguration#getAutoEditStrategies that returns
* a ScalaAutoIndentStrategy instead of a JavaAutoIndentStrategy.
*
* @see org.eclipse.jface.text.source.SourceViewerConfiguration#getAutoEditStrategies(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
*/
override def getAutoEditStrategies(sourceViewer: ISourceViewer, contentType: String): Array[IAutoEditStrategy] = {
val partitioning = getConfiguredDocumentPartitioning(sourceViewer)
contentType match {
case IJavaPartitions.JAVA_DOC | IJavaPartitions.JAVA_MULTI_LINE_COMMENT =>
Array(new JavaDocAutoIndentStrategy(partitioning))
case IJavaPartitions.JAVA_STRING =>
Array(new SmartSemicolonAutoEditStrategy(partitioning), new JavaStringAutoIndentStrategy(partitioning))
case IJavaPartitions.JAVA_CHARACTER | IDocument.DEFAULT_CONTENT_TYPE =>
Array(new SmartSemicolonAutoEditStrategy(partitioning), new ScalaAutoIndentStrategy(partitioning, getProject, sourceViewer, new JdtPreferenceProvider(getProject)), new AutoCloseBracketStrategy)
case _ =>
Array(new ScalaAutoIndentStrategy(partitioning, getProject, sourceViewer, new JdtPreferenceProvider(getProject)))
}
}
override def getContentFormatter(sourceViewer: ISourceViewer) = {
val formatter = new MultiPassContentFormatter(getConfiguredDocumentPartitioning(sourceViewer), IDocument.DEFAULT_CONTENT_TYPE)
formatter.setMasterStrategy(new ScalaFormattingStrategy(editor))
formatter
}
override def handlePropertyChangeEvent(event: PropertyChangeEvent) {
super.handlePropertyChangeEvent(event)
scalaCodeScanner.adaptToPreferenceChange(event)
scaladocScanner.adaptToPreferenceChange(event)
stringScanner.adaptToPreferenceChange(event)
multiLineStringScanner.adaptToPreferenceChange(event)
singleLineCommentScanner.adaptToPreferenceChange(event)
multiLineCommentScanner.adaptToPreferenceChange(event)
xmlTagScanner.adaptToPreferenceChange(event)
xmlCommentScanner.adaptToPreferenceChange(event)
xmlCDATAScanner.adaptToPreferenceChange(event)
xmlPCDATAScanner.adaptToPreferenceChange(event)
xmlPIScanner.adaptToPreferenceChange(event)
}
override def getConfiguredContentTypes(sourceViewer: ISourceViewer): Array[String] = {
// Adds the SCALA_MULTI_LINE_STRING partition type to the list of configured content types, so it is
// supported for the comment out and shift left/right actions
return super.getConfiguredContentTypes(sourceViewer) :+ ScalaPartitions.SCALA_MULTI_LINE_STRING
}
override def affectsTextPresentation(event: PropertyChangeEvent) = true
}