/*********************************************************************************************************************************** * Copyright (c) 2000, 2004 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: IBM Corporation - initial API and implementation **********************************************************************************************************************************/ package net.sourceforge.phpeclipse.wiki.editor; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.RGB; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.TextAttribute; import org.eclipse.jface.text.contentassist.ContentAssistant; import org.eclipse.jface.text.contentassist.IContentAssistProcessor; 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.reconciler.IReconciler; import org.eclipse.jface.text.reconciler.MonoReconciler; import org.eclipse.jface.text.rules.DefaultDamagerRepairer; import org.eclipse.jface.text.rules.IRule; import org.eclipse.jface.text.rules.IToken; import org.eclipse.jface.text.rules.ITokenScanner; import org.eclipse.jface.text.rules.IWordDetector; import org.eclipse.jface.text.rules.RuleBasedScanner; import org.eclipse.jface.text.rules.SingleLineRule; import org.eclipse.jface.text.rules.Token; import org.eclipse.jface.text.rules.WordRule; 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; public class WikiSourceViewerConfiguration extends SourceViewerConfiguration { /** * A no-op implementation of IAnnotationHover that will trigger the text editor to set up annotation hover support. */ private static final class NullHover implements IAnnotationHover { /* * @see org.eclipse.jface.text.source.IAnnotationHover#getHoverInfo(org.eclipse.jface.text.source.ISourceViewer, int) */ public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) { return null; } } /** * Simple word detector that detects any sequence of non-whitespace as a word. */ private static final class SimpleWordDetector implements IWordDetector { /* * @see org.eclipse.jface.text.rules.IWordDetector#isWordStart(char) */ public boolean isWordStart(char c) { return isWordPart(c); } /* * @see org.eclipse.jface.text.rules.IWordDetector#isWordPart(char) */ public boolean isWordPart(char c) { return !Character.isWhitespace(c); } } private static final class SimpleListDetector implements IWordDetector { /* * @see org.eclipse.jface.text.rules.IWordDetector#isWordStart(char) */ public boolean isWordStart(char c) { return c == '*' || c == '#'; } /* * @see org.eclipse.jface.text.rules.IWordDetector#isWordPart(char) */ public boolean isWordPart(char c) { return true; } } private final ISharedTextColors fColors; private WikiEditor fEditor; public WikiSourceViewerConfiguration(WikiEditor editor, ISharedTextColors colors) { fEditor = editor; fColors = colors; } /* * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getPresentationReconciler(org.eclipse.jface.text.source.ISourceViewer) */ public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) { PresentationReconciler reconciler = new PresentationReconciler(); DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getRecipeScanner()); reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE); reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE); return reconciler; } private ITokenScanner getRecipeScanner() { RuleBasedScanner scanner = new RuleBasedScanner(); IRule[] rules = new IRule[11]; rules[0] = createHeader6Rule(); rules[1] = createHeader5Rule(); rules[2] = createHeader4Rule(); rules[3] = createHeader3Rule(); rules[4] = createHeader2Rule(); rules[5] = createHeader1Rule(); rules[6] = createHRRule(); rules[7] = createLinkRule(); rules[8] = createExternalHTTPRule(); rules[9] = createListRule(); rules[10] = createNumberedListRule(); scanner.setRules(rules); return scanner; } // private IRule createSectionTitleRule() { // IToken titleToken= new Token(new TextAttribute(null, null, SWT.BOLD)); // WordRule wordRule= new WordRule(new SimpleWordDetector()); // wordRule.addWord(IngredientsSection.TITLE + ":", titleToken); // wordRule.addWord(PreparationSection.TITLE + ":", titleToken); // return wordRule; // } private IRule createHRRule() { IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.ITALIC)); SingleLineRule singleLineRule = new SingleLineRule("----", "\n", quantityToken); singleLineRule.setColumnConstraint(0); return singleLineRule; } private IRule createHeader1Rule() { IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.ITALIC)); SingleLineRule singleLineRule = new SingleLineRule("=", "=", quantityToken); singleLineRule.setColumnConstraint(0); return singleLineRule; } private IRule createHeader2Rule() { IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.ITALIC)); SingleLineRule singleLineRule = new SingleLineRule("==", "==", quantityToken); singleLineRule.setColumnConstraint(0); return singleLineRule; } private IRule createHeader3Rule() { IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.ITALIC)); SingleLineRule singleLineRule = new SingleLineRule("===", "===", quantityToken); singleLineRule.setColumnConstraint(0); return singleLineRule; } private IRule createHeader4Rule() { IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.ITALIC)); SingleLineRule singleLineRule = new SingleLineRule("====", "====", quantityToken); singleLineRule.setColumnConstraint(0); return singleLineRule; } private IRule createHeader5Rule() { IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.ITALIC)); SingleLineRule singleLineRule = new SingleLineRule("=====", "=====", quantityToken); singleLineRule.setColumnConstraint(0); return singleLineRule; } private IRule createHeader6Rule() { IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.ITALIC)); SingleLineRule singleLineRule = new SingleLineRule("======", "======", quantityToken); singleLineRule.setColumnConstraint(0); return singleLineRule; } // private IRule createLeadingDashRule() { // IToken dashToken= new Token(new TextAttribute(fColors.getColor(new RGB(200, 100, 100)), null, SWT.BOLD)); // WordRule wordRule= new WordRule(new SimpleWordDetector()); // wordRule.addWord("-", dashToken); // wordRule.setColumnConstraint(0); // return wordRule; // } private IRule createListRule() { IToken dashToken = new Token(new TextAttribute(fColors.getColor(new RGB(63, 127, 95)), null, SWT.BOLD)); // WordRule wordRule= new WordRule(new SimpleListDetector()); // wordRule.addWord("*", dashToken); SingleLineRule singleLineRule = new SingleLineRule("*", "\n", dashToken); singleLineRule.setColumnConstraint(0); return singleLineRule; } private IRule createNumberedListRule() { IToken dashToken = new Token(new TextAttribute(fColors.getColor(new RGB(63, 127, 95)), null, SWT.BOLD)); // WordRule wordRule= new WordRule(new SimpleListDetector()); // wordRule.addWord("#", dashToken); SingleLineRule singleLineRule = new SingleLineRule("#", "\n", dashToken); singleLineRule.setColumnConstraint(0); return singleLineRule; } private IRule createLinkRule() { IToken stepToken = new Token(new TextAttribute(fColors.getColor(new RGB(200, 100, 100)), null, SWT.BOLD)); SingleLineRule stepRule = new SingleLineRule("[[", "]]", stepToken); // stepRule.setColumnConstraint(0); return stepRule; } private IRule createExternalHTTPRule() { IToken stepToken = new Token(new TextAttribute(fColors.getColor(new RGB(200, 100, 100)), null, SWT.BOLD)); SingleLineRule stepRule = new SingleLineRule("[http", "]", stepToken); // stepRule.setColumnConstraint(0); return stepRule; } /* * @see SourceViewerConfiguration#getReconciler(ISourceViewer) */ public IReconciler getReconciler(ISourceViewer sourceViewer) { WikiReconcilingStrategy strategy = new WikiReconcilingStrategy(fEditor); MonoReconciler reconciler = new MonoReconciler(strategy, false); reconciler.setProgressMonitor(new NullProgressMonitor()); reconciler.setDelay(500); return reconciler; } /* * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getContentAssistant(org.eclipse.jface.text.source.ISourceViewer) */ public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) { ContentAssistant assistant = new ContentAssistant(); IContentAssistProcessor processor = new WikiCompletionProcessor(fEditor); assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE); assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE); assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer)); assistant.enableAutoInsert(true); return assistant; } /* * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getAnnotationHover(org.eclipse.jface.text.source.ISourceViewer) */ public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) { return new NullHover(); } }