1 /***********************************************************************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made
3 * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4 * http://www.eclipse.org/legal/cpl-v10.html
6 * Contributors: IBM Corporation - initial API and implementation
7 **********************************************************************************************************************************/
8 package net.sourceforge.phpeclipse.wiki.editor;
10 import org.eclipse.core.runtime.NullProgressMonitor;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.graphics.RGB;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.jface.text.TextAttribute;
17 import org.eclipse.jface.text.contentassist.ContentAssistant;
18 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
19 import org.eclipse.jface.text.contentassist.IContentAssistant;
20 import org.eclipse.jface.text.presentation.IPresentationReconciler;
21 import org.eclipse.jface.text.presentation.PresentationReconciler;
22 import org.eclipse.jface.text.reconciler.IReconciler;
23 import org.eclipse.jface.text.reconciler.MonoReconciler;
24 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
25 import org.eclipse.jface.text.rules.IRule;
26 import org.eclipse.jface.text.rules.IToken;
27 import org.eclipse.jface.text.rules.ITokenScanner;
28 import org.eclipse.jface.text.rules.IWordDetector;
29 import org.eclipse.jface.text.rules.RuleBasedScanner;
30 import org.eclipse.jface.text.rules.SingleLineRule;
31 import org.eclipse.jface.text.rules.Token;
32 import org.eclipse.jface.text.rules.WordRule;
33 import org.eclipse.jface.text.source.IAnnotationHover;
34 import org.eclipse.jface.text.source.ISharedTextColors;
35 import org.eclipse.jface.text.source.ISourceViewer;
36 import org.eclipse.jface.text.source.SourceViewerConfiguration;
38 public class WikiSourceViewerConfiguration extends SourceViewerConfiguration {
41 * A no-op implementation of <code>IAnnotationHover</code> that will trigger the text editor to set up annotation hover support.
43 private static final class NullHover implements IAnnotationHover {
46 * @see org.eclipse.jface.text.source.IAnnotationHover#getHoverInfo(org.eclipse.jface.text.source.ISourceViewer, int)
48 public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
54 * Simple word detector that detects any sequence of non-whitespace as a word.
56 private static final class SimpleWordDetector implements IWordDetector {
58 * @see org.eclipse.jface.text.rules.IWordDetector#isWordStart(char)
60 public boolean isWordStart(char c) {
65 * @see org.eclipse.jface.text.rules.IWordDetector#isWordPart(char)
67 public boolean isWordPart(char c) {
68 return !Character.isWhitespace(c);
72 private static final class SimpleListDetector implements IWordDetector {
74 * @see org.eclipse.jface.text.rules.IWordDetector#isWordStart(char)
76 public boolean isWordStart(char c) {
77 return c == '*' || c == '#';
81 * @see org.eclipse.jface.text.rules.IWordDetector#isWordPart(char)
83 public boolean isWordPart(char c) {
88 private final ISharedTextColors fColors;
90 private WikiEditor fEditor;
92 public WikiSourceViewerConfiguration(WikiEditor editor, ISharedTextColors colors) {
98 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getPresentationReconciler(org.eclipse.jface.text.source.ISourceViewer)
100 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
101 PresentationReconciler reconciler = new PresentationReconciler();
102 DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getRecipeScanner());
103 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
104 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
108 private ITokenScanner getRecipeScanner() {
109 RuleBasedScanner scanner = new RuleBasedScanner();
111 IRule[] rules = new IRule[14];
112 rules[0] = createHeader6Rule();
113 rules[1] = createHeader5Rule();
114 rules[2] = createHeader4Rule();
115 rules[3] = createHeader3Rule();
116 rules[4] = createHeader2Rule();
117 rules[5] = createHeader1Rule();
118 rules[6] = createHRRule();
119 rules[7] = createLinkRule();
120 rules[8] = createExternalHTTPRule();
121 rules[9] = createListRule();
122 rules[10] = createNumberedListRule();
123 rules[11] = createBoldItalicRule();
124 rules[12] = createBoldRule();
125 rules[13] = createItalicRule();
127 scanner.setRules(rules);
131 // private IRule createSectionTitleRule() {
132 // IToken titleToken= new Token(new TextAttribute(null, null, SWT.BOLD));
133 // WordRule wordRule= new WordRule(new SimpleWordDetector());
134 // wordRule.addWord(IngredientsSection.TITLE + ":", titleToken);
135 // wordRule.addWord(PreparationSection.TITLE + ":", titleToken);
139 private IRule createBoldRule() {
140 IToken boldToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 0)), null, SWT.BOLD));
141 SingleLineRule singleLineRule = new SingleLineRule("'''", "'''", boldToken);
142 return singleLineRule;
145 private IRule createItalicRule() {
146 IToken italicToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 0)), null, SWT.ITALIC));
147 SingleLineRule singleLineRule = new SingleLineRule("''", "''", italicToken);
148 return singleLineRule;
151 private IRule createBoldItalicRule() {
152 IToken boldToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 0)), null, SWT.BOLD | SWT.ITALIC));
153 SingleLineRule singleLineRule = new SingleLineRule("'''''", "'''''", boldToken);
154 return singleLineRule;
157 private IRule createHRRule() {
158 IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.NONE));
159 SingleLineRule singleLineRule = new SingleLineRule("----", "\n", quantityToken);
160 singleLineRule.setColumnConstraint(0);
161 return singleLineRule;
164 private IRule createHeader1Rule() {
165 IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.NONE));
166 SingleLineRule singleLineRule = new SingleLineRule("=", "=", quantityToken);
167 singleLineRule.setColumnConstraint(0);
168 return singleLineRule;
171 private IRule createHeader2Rule() {
172 IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.NONE));
173 SingleLineRule singleLineRule = new SingleLineRule("==", "==", quantityToken);
174 singleLineRule.setColumnConstraint(0);
175 return singleLineRule;
178 private IRule createHeader3Rule() {
179 IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.NONE));
180 SingleLineRule singleLineRule = new SingleLineRule("===", "===", quantityToken);
181 singleLineRule.setColumnConstraint(0);
182 return singleLineRule;
185 private IRule createHeader4Rule() {
186 IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.NONE));
187 SingleLineRule singleLineRule = new SingleLineRule("====", "====", quantityToken);
188 singleLineRule.setColumnConstraint(0);
189 return singleLineRule;
192 private IRule createHeader5Rule() {
193 IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.NONE));
194 SingleLineRule singleLineRule = new SingleLineRule("=====", "=====", quantityToken);
195 singleLineRule.setColumnConstraint(0);
196 return singleLineRule;
199 private IRule createHeader6Rule() {
200 IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.NONE));
201 SingleLineRule singleLineRule = new SingleLineRule("======", "======", quantityToken);
202 singleLineRule.setColumnConstraint(0);
203 return singleLineRule;
206 // private IRule createLeadingDashRule() {
207 // IToken dashToken= new Token(new TextAttribute(fColors.getColor(new RGB(200, 100, 100)), null, SWT.BOLD));
208 // WordRule wordRule= new WordRule(new SimpleWordDetector());
209 // wordRule.addWord("-", dashToken);
210 // wordRule.setColumnConstraint(0);
214 private IRule createListRule() {
215 IToken dashToken = new Token(new TextAttribute(fColors.getColor(new RGB(63, 127, 95)), null, SWT.NONE));
216 // WordRule wordRule= new WordRule(new SimpleListDetector());
217 // wordRule.addWord("*", dashToken);
218 SingleLineRule singleLineRule = new SingleLineRule("*", "\n", dashToken);
219 singleLineRule.setColumnConstraint(0);
220 return singleLineRule;
223 private IRule createNumberedListRule() {
224 IToken dashToken = new Token(new TextAttribute(fColors.getColor(new RGB(63, 127, 95)), null, SWT.NONE));
225 // WordRule wordRule= new WordRule(new SimpleListDetector());
226 // wordRule.addWord("#", dashToken);
227 SingleLineRule singleLineRule = new SingleLineRule("#", "\n", dashToken);
228 singleLineRule.setColumnConstraint(0);
229 return singleLineRule;
232 private IRule createLinkRule() {
233 IToken stepToken = new Token(new TextAttribute(fColors.getColor(new RGB(200, 100, 100)), null, SWT.NONE));
234 SingleLineRule stepRule = new SingleLineRule("[[", "]]", stepToken);
235 // stepRule.setColumnConstraint(0);
239 private IRule createExternalHTTPRule() {
240 IToken stepToken = new Token(new TextAttribute(fColors.getColor(new RGB(200, 100, 100)), null, SWT.NONE));
241 SingleLineRule stepRule = new SingleLineRule("[http", "]", stepToken);
242 // stepRule.setColumnConstraint(0);
247 * @see SourceViewerConfiguration#getReconciler(ISourceViewer)
249 public IReconciler getReconciler(ISourceViewer sourceViewer) {
250 WikiReconcilingStrategy strategy = new WikiReconcilingStrategy(fEditor);
251 MonoReconciler reconciler = new MonoReconciler(strategy, false);
252 reconciler.setProgressMonitor(new NullProgressMonitor());
253 reconciler.setDelay(500);
259 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getContentAssistant(org.eclipse.jface.text.source.ISourceViewer)
261 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
262 ContentAssistant assistant = new ContentAssistant();
264 IContentAssistProcessor processor = new WikiCompletionProcessor(fEditor);
265 assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
267 assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
268 assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
269 assistant.enableAutoInsert(true);
275 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getAnnotationHover(org.eclipse.jface.text.source.ISourceViewer)
277 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
278 return new NullHover();