nescsourceviewerconfiguration.java

来自「plugin for eclipse」· Java 代码 · 共 220 行

JAVA
220
字号
package isis.tinydt.editors.nesceditor;


import isis.tinydt.TinydtPlugin;
import isis.tinydt.TinydtProject;
import isis.tinydt.Utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.TextPresentation;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.presentation.IPresentationDamager;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.IPresentationRepairer;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.reconciler.DirtyRegion;
import org.eclipse.jface.text.reconciler.IReconciler;
import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
import org.eclipse.jface.text.reconciler.MonoReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.texteditor.ITextEditor;




public class NesCSourceViewerConfiguration extends SourceViewerConfiguration 
{    	 
	private ITextEditor editor;
	
	class NesCDamagerRepairer implements IPresentationDamager, IPresentationRepairer 
	{
		public IRegion getDamageRegion(ITypedRegion arg0, DocumentEvent arg1, boolean arg2) 
		{
			return null;
		}
		
		public void setDocument(IDocument document) 
		{			
		}

		public void createPresentation(TextPresentation pres, ITypedRegion arg1) 
		{
            TextAttribute attr= new TextAttribute(Display.getDefault().getSystemColor(SWT.COLOR_RED), null, SWT.BOLD);            
			pres.addStyleRange( new StyleRange( 10, 20, attr.getForeground(), attr.getBackground(), attr.getStyle() ));
		}
	}
		
	class NesCReconcilingStrategy implements IReconcilingStrategy    
	{
		private IDocument   document;
        private ITextEditor editor;
		
		public NesCReconcilingStrategy(ITextEditor editor)
		{
            this.editor = editor;
		}
		
		public void reconcile(DirtyRegion arg0, IRegion arg1) 
		{
			reconcile();
		}

		public void reconcile(IRegion arg0) 
		{
			reconcile();
		}

		public void setDocument(IDocument document) 
		{
			this.document = document; 
		}
		
		protected void reconcile()
		{
            // add "." to filename (eg.: c:\temp\test.nc -> c:\temp\.test.nc) 
            IFileEditorInput input = (IFileEditorInput)editor.getEditorInput();
            IPath loc = input.getFile().getLocation();            
            String file_name = Utils.addDotToFileName(input.getFile().getLocation().toString());           
            
            // write text to file, so the parser will read it first 
            String text = editor.getDocumentProvider().getDocument(input).get();            
            try
            {                
                BufferedWriter w = new BufferedWriter( new FileWriter(file_name));
                w.write(text);
                w.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            
            TinydtProject p = TinydtProject.getTinydtProject(input.getFile().getProject());
            p.parseAll();
            
            new File(file_name).delete();
		}
	}

	
	public NesCSourceViewerConfiguration(ITextEditor editor) 
	{
		this.editor = editor;				
	}
    
    public IContentAssistant getContentAssistant(ISourceViewer sourceViewer)
    {
        ContentAssistant assistant = new ContentAssistant();
        
        assistant.enableAutoActivation(true);
        assistant.setAutoActivationDelay(500);
        //assistant.enablePrefixCompletion(true);
        
        
        //assistant.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
        assistant.setContentAssistProcessor( new NesCCodeCompletionProcessor(editor), IDocument.DEFAULT_CONTENT_TYPE );
        
        //assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
        return assistant;
    }
	
	public IReconciler getReconciler(ISourceViewer sourceViewer) 
	{
		NesCReconcilingStrategy strategy = new NesCReconcilingStrategy(editor);
		
		MonoReconciler reconciler= new MonoReconciler(strategy,false);
		reconciler.setIsIncrementalReconciler(false);
		reconciler.setProgressMonitor(new NullProgressMonitor());
		reconciler.setDelay(500);

		return reconciler;		
	}
	
	/*public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) 
	{	
		NesCRuleBasedScanner scanner = new NesCRuleBasedScanner();
		Color default_tag_color = new Color(Display.getCurrent(), new RGB(0, 0, 0));
		scanner.setDefaultReturnToken( new Token(new TextAttribute(default_tag_color)));	    			
		PresentationReconciler reconciler = new PresentationReconciler();
		DefaultDamagerRepairer dr = new DefaultDamagerRepairer(scanner);
		reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
		reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
		return reconciler;
	}*/
	
	/*public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) 
	{
		ContentAssistant assistant = new ContentAssistant();
		assistant.setContentAssistProcessor( new MyCompletionProcessor(), IDocument.DEFAULT_CONTENT_TYPE);
		assistant.enableAutoActivation(true);
		assistant.setAutoActivationDelay(500);
		assistant.setProposalPopupOrientation(
			IContentAssistant.PROPOSAL_OVERLAY);
		return assistant;
	}*/
	
	 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) 
     {
	      
	      PresentationReconciler reconciler = new PresentationReconciler();
	      
	      // ??????????????
	      NesCColorProvider colorProvider = TinydtPlugin.getDefault().getColorProvider();;
	      DefaultDamagerRepairer commentDR =
	        new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(colorProvider.getColor(NesCColorProvider.SINGLE_LINE_COMMENT))));
	      reconciler.setDamager(commentDR, NesCPartitionScanner.NESC_COMMENT);
	      reconciler.setRepairer(commentDR, NesCPartitionScanner.NESC_COMMENT);
	      
	      // ???????????????
	      DefaultDamagerRepairer stringDR =
	        new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(colorProvider.getColor(NesCColorProvider.STRING))));
	      reconciler.setDamager(stringDR, NesCPartitionScanner.NESC_STRING);
	      reconciler.setRepairer(stringDR, NesCPartitionScanner.NESC_STRING);
	      
	      
	      // ???????????????
	      DefaultDamagerRepairer keywordDR = new DefaultDamagerRepairer(new NesCKeywordPartitionScanner());
	      reconciler.setDamager(keywordDR, IDocument.DEFAULT_CONTENT_TYPE);
	      reconciler.setRepairer(keywordDR, IDocument.DEFAULT_CONTENT_TYPE);
	      
	      return reconciler;
        
           /* NesCRuleBasedScanner scanner = new NesCRuleBasedScanner();
            Color default_tag_color = new Color(Display.getCurrent(), new RGB(0, 0, 0));
            scanner.setDefaultReturnToken( new Token(new TextAttribute(default_tag_color)));                    
            PresentationReconciler reconciler = new PresentationReconciler();
            DefaultDamagerRepairer dr = new DefaultDamagerRepairer(scanner);
            reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
            reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
            return reconciler;*/
     }
     
     public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) 
     {
        return new String[] {
            IDocument.DEFAULT_CONTENT_TYPE,
            NesCPartitionScanner.NESC_COMMENT,
            NesCPartitionScanner.NESC_STRING
          };
     }
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?