【问题标题】:(Eclipse) Syntax Highlighting Plugin, odd issues with getting started on highlighting/validation(Eclipse)语法突出显示插件,开始突出显示/验证的奇怪问题
【发布时间】:2018-08-11 02:05:50
【问题描述】:

我正在为我的公司制作一个语法高亮插件。我没有制作新的编辑器,我使用的是使用通用编辑器扩展的 Eclipse 模板。

免责声明:由于版本控制问题、丢失文件等,我在让 XText 在 3 台不同的机器上一致工作时遇到了很多麻烦,所以这是不可能的

org.eclipse.ui.genericeditor.presentationReconcilers
org.eclipse.ui.genericeditor.contentTypes
org.eclipse.ui.genericeditor.hoverProviders
org.eclipse.ui.genericeditor.contentAssistProcessors
org.eclipse.ui.editors
org.eclipse.core.filebuffers.documentSetup

我正在使用我遇到了一些奇怪的问题。在我开始之前:

  • 在帮助 > 关于 Eclipse > 安装详细信息 > 插件中检测到插件 其他示例插件使用与我运行此插件相同的方法运行

问题:

  • 我在放置任何内容时收到以下错误消息 除<?xml version='1.0'> 之外的文件:

    “Prolog 中不允许的内容”

  • 关键字似乎以蓝色突出显示。正如你在我的 源代码,它们应该以红色突出显示。

    Click here 查看我的运行时 Ecipe 编辑器的样子 当我尝试测试我的语法规则时。

以下是我的课程源代码。 我想知道:

  1. 为什么我的关键字在语法着色中被识别但 上面有“prolog”错误的无效命令
  2. 为什么会出现上述 prolog 错误
  3. 为什么它不是根据我的规范或其他规范验证文件
  4. 为我指明正确的方向

希望您能提供帮助。 谢谢:)。

协调器类:

package myplugin;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;


public class MyReconciler extends PresentationReconciler {

    public MyReconciler() {
        // TODO this is logic for .project file to color tags in blue. Replace with your language logic!
        MyScanner scanner = new MyScanner(new SyntaxColorProvider());

        DefaultDamagerRepairer dr= new DefaultDamagerRepairer(scanner);
        this.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
        this.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
    }
}

扫描仪类:

package myplugin;

import java.util.ArrayList; 
import java.util.List; 

import org.eclipse.jface.text.TextAttribute; 
import org.eclipse.jface.text.rules.BufferedRuleBasedScanner; 
import org.eclipse.jface.text.rules.EndOfLineRule; 
import org.eclipse.jface.text.rules.IRule; 
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.IWordDetector;
import org.eclipse.jface.text.rules.MultiLineRule;
import org.eclipse.jface.text.rules.PatternRule; 
import org.eclipse.jface.text.rules.SingleLineRule; 
import org.eclipse.jface.text.rules.Token; 
import org.eclipse.jface.text.rules.WhitespaceRule; 
import org.eclipse.jface.text.rules.WordRule; 


public class MyScanner extends BufferedRuleBasedScanner 
{ 

private static String[] misc = {
     true",
    "false", 
    "unsigned",
    "jump",
    "read",     
    "write",
};

// Tokens
private final IToken KEYWORD_TOKEN;

private List<IRule> basicRules;

public MyScanner(SyntaxColorProvider colorProvider)
{
    super(5000);

    // CREATE TOKENS
    KEYWORD_TOKEN = new Token(new TextAttribute(colorProvider.getColor(SyntaxColorProvider.KEYWORD)));

    // CREATE RULES
    List rules = new ArrayList<IRule>();


    // Add rule for strings and character constants.
    rules.add(new SingleLineRule("\"", "\"", STRING_TOKEN, '\\'));
    rules.add(new SingleLineRule("'", "'", STRING_TOKEN, '\\'));

    // Add word rule for keywords, types, and constants.
    WordRule wordRule = new WordRule(new WordDetector(), OTHER_TOKEN);

    // Single-line comments
    rules.add(new EndOfLineRule("//", STRING_TOKEN));

    // Multi-line comments
    rules.add(new MultiLineRule("/$", "$/", COMMENT_TOKEN));

    // KEYWORDS
    for (String misc : misc)
    {
        wordRule.addWord(misc, KEYWORD_TOKEN);
    }
    rules.add(wordRule);

    IRule[] result= new IRule[rules.size()];
    rules.toArray(result);
    setRules(result);
}

}

ValidatorDocumentSetupParticipant:

package myplugin;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.eclipse.core.filebuffers.IDocumentSetupParticipant;
import org.eclipse.core.filebuffers.IDocumentSetupParticipantExtension;
import org.eclipse.core.filebuffers.LocationKind;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;

public class ValidatorDocumentSetupParticipant implements IDocumentSetupParticipant, IDocumentSetupParticipantExtension {

    private final class DocumentValidator implements IDocumentListener {
        private final IFile file;
        private IMarker marker;

        private DocumentValidator(IFile file) {
            this.file = file;
        }

        @Override
        public void documentChanged(DocumentEvent event) {
            if (this.marker != null) {
                try {
                    this.marker.delete();
                } catch (CoreException e) {
                    e.printStackTrace();
                }
                this.marker = null;
            }
            try (StringReader reader = new StringReader(event.getDocument().get());) {
                DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                documentBuilder.parse(new InputSource(reader));
            } catch (Exception ex) {
                try {
                    this.marker = file.createMarker(IMarker.PROBLEM);
                    this.marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
                    this.marker.setAttribute(IMarker.MESSAGE, ex.getMessage());
                    if (ex instanceof SAXParseException) {
                        SAXParseException saxParseException = (SAXParseException)ex;
                        int lineNumber = saxParseException.getLineNumber();
                        int offset = event.getDocument().getLineInformation(lineNumber - 1).getOffset() + saxParseException.getColumnNumber() - 1;
                        this.marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
                        this.marker.setAttribute(IMarker.CHAR_START, offset);
                        this.marker.setAttribute(IMarker.CHAR_END, offset + 1);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void documentAboutToBeChanged(DocumentEvent event) {
        }
    }

    @Override
    public void setup(IDocument document) {
    }

    @Override
    public void setup(IDocument document, IPath location, LocationKind locationKind) {
        if (locationKind == LocationKind.IFILE) {
            IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(location);
            document.addDocumentListener(new DocumentValidator(file));
        }

WordDetector 类:

 package myplugin;

import org.eclipse.jface.text.rules.IWordDetector; 

public class WordDetector implements IWordDetector 
{    
    public boolean isWordPart(char character) {
        return Character.isJavaIdentifierPart(character);
    }

    public boolean isWordStart(char character) {
        return Character.isJavaIdentifierStart(character);
    }
}

SyntaxColorProvider 类:

package myplugin;

import java.util.HashMap; 

public class SyntaxColorProvider 
{ 

public static final RGB RED = new RGB(200, 0, 0); 

public static final RGB GREEN = new RGB(0, 200, 0); 

public static final RGB BLUE = new RGB(0, 0, 200); 

public static final RGB COMMENT = new RGB(128, 128, 128);  

public static final RGB KEYWORD = new RGB(255, 0, 0); 

public static final RGB TYPE = new RGB(0, 0, 128); 

public static final RGB STRING = new RGB(0, 128, 0); 

public static final RGB DEFAULT = new RGB(0, 0, 0); 


protected Map fColorTable = new HashMap(10); 

/**
 * Release all of the color resources held onto by the receiver. 
 */ 
public void dispose() 
{ 
    Iterator e = fColorTable.values().iterator(); 
    while (e.hasNext()) 
        ((Color) e.next()).dispose(); 
} 

/**
 * Return the Color that is stored in the Color table as rgb. 
 */ 
public Color getColor(RGB rgb) 
{ 
    Color color = (Color) fColorTable.get(rgb); 
    if (color == null) 
    { 
        color = new Color(Display.getCurrent(), rgb); 
        fColorTable.put(rgb, color); 
    } 
    return color; 
}

【问题讨论】:

    标签: eclipse validation plugins syntax highlighting


    【解决方案1】:

    这看起来像是带有一些更改的示例编辑器 - 并且

    documentBuilder.parse(new InputSource(reader));
    

    也许会解析 XML。它检测到不正确的 XML,因为缺少序言。从代码中删除该行,或实现类似的标记和标记问题的东西。

    【讨论】:

      【解决方案2】:

      好的,所以我一直在努力解决同样的问题,导致这个问题的函数在这一行:documentBuilder.parse(new InputSource(reader));,我正在尝试自己找出一个文本解析器。

      【讨论】:

        猜你喜欢
        • 2018-05-28
        • 2011-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多