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[11];
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();
124 scanner.setRules(rules);
128 // private IRule createSectionTitleRule() {
129 // IToken titleToken= new Token(new TextAttribute(null, null, SWT.BOLD));
130 // WordRule wordRule= new WordRule(new SimpleWordDetector());
131 // wordRule.addWord(IngredientsSection.TITLE + ":", titleToken);
132 // wordRule.addWord(PreparationSection.TITLE + ":", titleToken);
136 private IRule createHRRule() {
137 IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.ITALIC));
138 SingleLineRule singleLineRule = new SingleLineRule("----", "\n", quantityToken);
139 singleLineRule.setColumnConstraint(0);
140 return singleLineRule;
143 private IRule createHeader1Rule() {
144 IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.ITALIC));
145 SingleLineRule singleLineRule = new SingleLineRule("=", "=", quantityToken);
146 singleLineRule.setColumnConstraint(0);
147 return singleLineRule;
150 private IRule createHeader2Rule() {
151 IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.ITALIC));
152 SingleLineRule singleLineRule = new SingleLineRule("==", "==", quantityToken);
153 singleLineRule.setColumnConstraint(0);
154 return singleLineRule;
157 private IRule createHeader3Rule() {
158 IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.ITALIC));
159 SingleLineRule singleLineRule = new SingleLineRule("===", "===", quantityToken);
160 singleLineRule.setColumnConstraint(0);
161 return singleLineRule;
164 private IRule createHeader4Rule() {
165 IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.ITALIC));
166 SingleLineRule singleLineRule = new SingleLineRule("====", "====", quantityToken);
167 singleLineRule.setColumnConstraint(0);
168 return singleLineRule;
171 private IRule createHeader5Rule() {
172 IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.ITALIC));
173 SingleLineRule singleLineRule = new SingleLineRule("=====", "=====", quantityToken);
174 singleLineRule.setColumnConstraint(0);
175 return singleLineRule;
178 private IRule createHeader6Rule() {
179 IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.ITALIC));
180 SingleLineRule singleLineRule = new SingleLineRule("======", "======", quantityToken);
181 singleLineRule.setColumnConstraint(0);
182 return singleLineRule;
185 // private IRule createLeadingDashRule() {
186 // IToken dashToken= new Token(new TextAttribute(fColors.getColor(new RGB(200, 100, 100)), null, SWT.BOLD));
187 // WordRule wordRule= new WordRule(new SimpleWordDetector());
188 // wordRule.addWord("-", dashToken);
189 // wordRule.setColumnConstraint(0);
193 private IRule createListRule() {
194 IToken dashToken = new Token(new TextAttribute(fColors.getColor(new RGB(63, 127, 95)), null, SWT.BOLD));
195 // WordRule wordRule= new WordRule(new SimpleListDetector());
196 // wordRule.addWord("*", dashToken);
197 SingleLineRule singleLineRule = new SingleLineRule("*", "\n", dashToken);
198 singleLineRule.setColumnConstraint(0);
199 return singleLineRule;
202 private IRule createNumberedListRule() {
203 IToken dashToken = new Token(new TextAttribute(fColors.getColor(new RGB(63, 127, 95)), null, SWT.BOLD));
204 // WordRule wordRule= new WordRule(new SimpleListDetector());
205 // wordRule.addWord("#", dashToken);
206 SingleLineRule singleLineRule = new SingleLineRule("#", "\n", dashToken);
207 singleLineRule.setColumnConstraint(0);
208 return singleLineRule;
211 private IRule createLinkRule() {
212 IToken stepToken = new Token(new TextAttribute(fColors.getColor(new RGB(200, 100, 100)), null, SWT.BOLD));
213 SingleLineRule stepRule = new SingleLineRule("[[", "]]", stepToken);
214 // stepRule.setColumnConstraint(0);
218 private IRule createExternalHTTPRule() {
219 IToken stepToken = new Token(new TextAttribute(fColors.getColor(new RGB(200, 100, 100)), null, SWT.BOLD));
220 SingleLineRule stepRule = new SingleLineRule("[http", "]", stepToken);
221 // stepRule.setColumnConstraint(0);
226 * @see SourceViewerConfiguration#getReconciler(ISourceViewer)
228 public IReconciler getReconciler(ISourceViewer sourceViewer) {
229 WikiReconcilingStrategy strategy = new WikiReconcilingStrategy(fEditor);
230 MonoReconciler reconciler = new MonoReconciler(strategy, false);
231 reconciler.setProgressMonitor(new NullProgressMonitor());
232 reconciler.setDelay(500);
238 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getContentAssistant(org.eclipse.jface.text.source.ISourceViewer)
240 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
241 ContentAssistant assistant = new ContentAssistant();
243 IContentAssistProcessor processor = new WikiCompletionProcessor(fEditor);
244 assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
246 assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
247 assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
248 assistant.enableAutoInsert(true);
254 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getAnnotationHover(org.eclipse.jface.text.source.ISourceViewer)
256 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
257 return new NullHover();