Search in sources :

Example 1 with ITSIssueType

use of org.languagetool.rules.ITSIssueType in project languagetool by languagetool-org.

the class LanguageToolSupport method updateHighlights.

private void updateHighlights() {
    removeHighlights();
    Highlighter h = textComponent.getHighlighter();
    for (Span span : documentSpans) {
        if (span.start == span.end) {
            continue;
        }
        try {
            if (span.start < span.end) {
                //to avoid the BadLocationException
                ITSIssueType issueType = span.rule.getLocQualityIssueType();
                Color colorForIssueType = getConfig().getErrorColors().get(issueType);
                Color bgColor = colorForIssueType != null ? colorForIssueType : null;
                Color underlineColor = ITSIssueType.Misspelling == span.rule.getLocQualityIssueType() ? Color.red : Color.blue;
                HighlightPainter painter = new HighlightPainter(bgColor, underlineColor);
                h.addHighlight(span.start, span.end, painter);
            }
        } catch (BadLocationException ex) {
            ex.printStackTrace();
        }
    }
}
Also used : ITSIssueType(org.languagetool.rules.ITSIssueType)

Example 2 with ITSIssueType

use of org.languagetool.rules.ITSIssueType in project languagetool by languagetool-org.

the class Configuration method parseErrorColors.

private void parseErrorColors(String colorsString) {
    if (StringUtils.isNotEmpty(colorsString)) {
        String[] typeToColorList = colorsString.split(",\\s*");
        for (String typeToColor : typeToColorList) {
            String[] typeAndColor = typeToColor.split(":");
            if (typeAndColor.length != 2) {
                throw new RuntimeException("Could not parse type and color, colon expected: '" + typeToColor + "'");
            }
            ITSIssueType type = ITSIssueType.getIssueType(typeAndColor[0]);
            String hexColor = typeAndColor[1];
            errorColors.put(type, Color.decode(hexColor));
        }
    }
}
Also used : ITSIssueType(org.languagetool.rules.ITSIssueType)

Example 3 with ITSIssueType

use of org.languagetool.rules.ITSIssueType in project languagetool by languagetool-org.

the class RuleMatchAsXmlSerializer method ruleMatchesToXmlSnippet.

/**
   * Get the XML snippet (i.e. not a complete XML document) for the given rules.
   * @see #getXmlStart
   * @see #getXmlEnd()
   */
public String ruleMatchesToXmlSnippet(List<RuleMatch> ruleMatches, String text, int contextSize) {
    StringBuilder xml = new StringBuilder(CAPACITY);
    //
    // IMPORTANT: people rely on this format, don't change it!
    //
    ContextTools contextTools = new ContextTools();
    contextTools.setEscapeHtml(false);
    contextTools.setContextSize(contextSize);
    String startMarker = "__languagetool_start_marker";
    contextTools.setErrorMarkerStart(startMarker);
    contextTools.setErrorMarkerEnd("");
    for (RuleMatch match : ruleMatches) {
        String subId = "";
        if (match.getRule() instanceof AbstractPatternRule) {
            AbstractPatternRule pRule = (AbstractPatternRule) match.getRule();
            if (pRule.getSubId() != null) {
                subId = " subId=\"" + escapeXMLForAPIOutput(pRule.getSubId()) + "\" ";
            }
        }
        xml.append("<error fromy=\"").append(match.getLine()).append('"').append(" fromx=\"").append(match.getColumn() - 1).append('"').append(" toy=\"").append(match.getEndLine()).append('"').append(" tox=\"").append(match.getEndColumn() - 1).append('"').append(" ruleId=\"").append(match.getRule().getId()).append('"');
        xml.append(subId);
        String msg = match.getMessage().replaceAll("</?suggestion>", "'");
        xml.append(" msg=\"").append(escapeXMLForAPIOutput(msg)).append('"');
        if (!match.getShortMessage().isEmpty()) {
            xml.append(" shortmsg=\"").append(escapeXMLForAPIOutput(match.getShortMessage())).append('"');
        }
        xml.append(" replacements=\"").append(escapeXMLForAPIOutput(String.join("#", match.getSuggestedReplacements()))).append('"');
        String context = contextTools.getContext(match.getFromPos(), match.getToPos(), text);
        // get position of error in context and remove artificial marker again:
        int contextOffset = context.indexOf(startMarker);
        context = context.replaceFirst(startMarker, "");
        context = context.replaceAll("[\n\r]", " ");
        xml.append(" context=\"").append(escapeForXmlAttribute(context)).append('"').append(" contextoffset=\"").append(contextOffset).append('"').append(" offset=\"").append(match.getFromPos()).append('"').append(" errorlength=\"").append(match.getToPos() - match.getFromPos()).append('"');
        if (match.getRule().getUrl() != null) {
            xml.append(" url=\"").append(escapeXMLForAPIOutput(match.getRule().getUrl().toString())).append('"');
        }
        Category category = match.getRule().getCategory();
        if (category != null) {
            xml.append(" category=\"").append(escapeXMLForAPIOutput(category.getName())).append('"');
            CategoryId id = category.getId();
            if (id != null) {
                xml.append(" categoryid=\"").append(escapeXMLForAPIOutput(id.toString())).append('"');
            }
        }
        ITSIssueType type = match.getRule().getLocQualityIssueType();
        if (type != null) {
            xml.append(" locqualityissuetype=\"").append(escapeXMLForAPIOutput(type.toString())).append('"');
        }
        xml.append("/>\n");
    }
    return xml.toString();
}
Also used : CategoryId(org.languagetool.rules.CategoryId) RuleMatch(org.languagetool.rules.RuleMatch) Category(org.languagetool.rules.Category) ITSIssueType(org.languagetool.rules.ITSIssueType) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule)

Aggregations

ITSIssueType (org.languagetool.rules.ITSIssueType)3 Category (org.languagetool.rules.Category)1 CategoryId (org.languagetool.rules.CategoryId)1 RuleMatch (org.languagetool.rules.RuleMatch)1 AbstractPatternRule (org.languagetool.rules.patterns.AbstractPatternRule)1