Skip to content

Commit

Permalink
Re-implementation of AnnotatedString
Browse files Browse the repository at this point in the history
This is a major commit that results from a complete rewrite of the AnnotatedString class, with the goal of closing issue #50 --which has been opened more than three years ago (!).

The new version of AnnotatedString relies on the Petit Poucet lineage tracking library, and many of its methods directly leverage the string manipulation functions that it provides. Contrary to what #50 suggested, the new class has richer functionalities, such as multiline regex find/replace, and its modular design should hopefully make it more robust than its predecessor. Thus, operations that required a line-by-line handling in the previous version can now be taken care of with a single global regex pattern. Line extraction is still supported, but in the longer term rules that work in this way could (and should) be refactored.
  • Loading branch information
sylvainhalle committed Dec 23, 2021
1 parent f208137 commit 4676594
Show file tree
Hide file tree
Showing 57 changed files with 2,020 additions and 2,076 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*.swp
*.bak
*.old
\#*

# MacOS !#@$ files
.DS_Store
Expand Down
82 changes: 50 additions & 32 deletions Source/Core/src/ca/uqac/lif/textidote/Advice.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
TeXtidote, a linter for LaTeX documents
Copyright (C) 2018 Sylvain Hallé
Copyright (C) 2018-2021 Sylvain Hallé
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -19,7 +19,10 @@

import java.util.List;

import ca.uqac.lif.textidote.as.Range;
import ca.uqac.lif.petitpoucet.function.strings.Range;
import ca.uqac.lif.textidote.as.AnnotatedString;
import ca.uqac.lif.textidote.as.AnnotatedString.Line;
import ca.uqac.lif.textidote.as.PositionRange;

/**
* A comment or suggestion on a portion of text. An advice applies to
Expand All @@ -31,6 +34,11 @@
*/
public class Advice implements Comparable<Advice>
{
/**
* The string on which this advice applies.
*/
protected AnnotatedString m_reference;

/**
* The range in the file where the advice applies
*/
Expand All @@ -40,7 +48,7 @@ public class Advice implements Comparable<Advice>
* The message associated with the advice
*/
protected String m_message;

/**
* The short message associated with the advice
*/
Expand All @@ -51,22 +59,11 @@ public class Advice implements Comparable<Advice>
*/
protected Rule m_rule;

/**
* The name of the resource (e.g. filename) this advice refers to
*/
protected String m_resource;

/**
* The line where the advice applies
*/
protected String m_line;

/**
* The number of characters from the beginning of the original
* text
*/
protected int m_offset = -1;

protected Line m_line;

/**
* A list of possible replacements for this message
*/
Expand All @@ -82,36 +79,57 @@ public class Advice implements Comparable<Advice>
* @param rule The rule from which this advice was generated
* @param range The range in the file where the advice applies
* @param message The message associated with the advice
* @param resource The name of the resource (e.g. filename) this
* @param resource The resource (e.g. filename) this
* advice refers to
* @param line The line of text on which the advice applies
* @param offset The position (in characters from the beginning of the
* text) where this advices starts
* text) where this advice starts
*/
public Advice(/*@ non_null @*/ Rule rule, /*@ non_null @*/ Range range,
/*@ non_null @*/ String message, /*@ non_null @*/ String resource,
/*@ non_null @*/ String line, int offset)
/*@ non_null @*/ String message, /*@ non_null @*/ AnnotatedString resource,
/*@ non_null @*/ Line line)
{
super();
m_rule = rule;
m_range = range;
m_message = message;
m_resource = resource;
m_reference = resource;
m_line = line;
m_replacements = null;
m_offset = offset;
m_shortMessage = "TeXtidote rule";
}

/**
* Gets the range in the file where the advice applies
* Gets the reference string this advice applies to
* @return The string
*/
/*@ non_null @*/ public AnnotatedString getReferenceString()
{
return m_reference;
}

/**
* Gets the linear range in the file where the advice applies
* @return The range
*/
/*@ pure non_null @*/ public Range getRange()
{
return m_range;
}

/**
* Gets the line/column range in the file where the advice applies
* @return The range
*/
/*@ pure non_null @*/ public PositionRange getPositionRange()
{
if (m_originalRange)
{
return m_reference.getOriginalPositionRange(m_range.getStart(), m_range.getEnd());
}
return m_reference.getPositionRange(m_range.getStart(), m_range.getEnd());
}

/**
* Sets whether the range applies on the original text or a sanitized
* version
Expand All @@ -123,7 +141,7 @@ public Advice setOriginal(boolean b)
m_originalRange = b;
return this;
}

/**
* Sets a short message for this advice
* @param message The message
Expand All @@ -132,7 +150,7 @@ public void setShortMessage(/*@ non_null @*/ String message)
{
m_shortMessage = message;
}

/**
* Sets a list of replacements for this advice
* @param replacements The replacements
Expand All @@ -141,7 +159,7 @@ public void setReplacements(/*@ null @*/ List<String> replacements)
{
m_replacements = replacements;
}

/**
* Gets a list of replacements for this advice
* @return The replacements; may be null
Expand All @@ -155,7 +173,7 @@ public void setReplacements(/*@ null @*/ List<String> replacements)
* Gets the line in the file where the advice applies
* @return The line
*/
/*@ pure non_null @*/ public String getLine()
/*@ pure non_null @*/ public Line getLine()
{
return m_line;
}
Expand All @@ -168,7 +186,7 @@ public void setReplacements(/*@ null @*/ List<String> replacements)
{
return m_message;
}

/**
* Gets the short message for this advice
* @return The short message
Expand All @@ -193,7 +211,7 @@ public void setReplacements(/*@ null @*/ List<String> replacements)
*/
/*@ pure non_null @*/ public String getResource()
{
return m_resource;
return m_reference.getResourceName();
}

@Override
Expand Down Expand Up @@ -222,22 +240,22 @@ public void setReplacements(/*@ null @*/ List<String> replacements)
}
Advice a = (Advice) o;
return m_range.equals(a.m_range) && m_rule.equals(a.m_rule)
&& m_resource.compareTo(a.m_resource) == 0;
&& m_reference.getResourceName().compareTo(a.getResource()) == 0;
}

@Override
public int compareTo(Advice a)
{
return m_range.compareTo(a.m_range);
}

/**
* Gets the offset corresponding to the start of the advice
* @return The number of characters from the beginning of the original
* text
*/
public int getOffset()
{
return m_offset;
return m_line.getOffset();
}
}
8 changes: 4 additions & 4 deletions Source/Core/src/ca/uqac/lif/textidote/Linter.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
public class Linter
{
/**
* The list of rules that will be evaluated
* The list of rules that will be evaluated on the original text
*/
protected List<Rule> m_rules;

/**
* The list of rules that will be evaluated
* The list of rules that will be evaluated on the plain text
*/
protected List<Rule> m_rulesDetexed;

Expand Down Expand Up @@ -188,12 +188,12 @@ protected List<String> getMatchingRules(String rule_pattern)
AnnotatedString s_decommented = m_cleaner.cleanComments(new AnnotatedString(s));
for (Rule r : m_rules)
{
filterAdvice(out_list, r.evaluate(s_decommented, s));
filterAdvice(out_list, r.evaluate(s_decommented));
}
AnnotatedString s_detexed = m_cleaner.clean(s);
for (Rule r : m_rulesDetexed)
{
filterAdvice(out_list, r.evaluate(s_detexed, s));
filterAdvice(out_list, r.evaluate(s_detexed));
}
return out_list;
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/src/ca/uqac/lif/textidote/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
import java.util.Scanner;
import java.util.Set;

import ca.uqac.lif.petitpoucet.function.strings.Range;
import ca.uqac.lif.textidote.as.AnnotatedString;
import ca.uqac.lif.textidote.as.Range;
import ca.uqac.lif.textidote.cleaning.CompositeCleaner;
import ca.uqac.lif.textidote.cleaning.ReplacementCleaner;
import ca.uqac.lif.textidote.cleaning.TextCleanerException;
Expand Down
4 changes: 1 addition & 3 deletions Source/Core/src/ca/uqac/lif/textidote/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ public abstract class Rule
/**
* Evaluates the rule on a string
* @param s The string on which to evaluate the rule
* @param original The original (untransformed) piece of text
* @return A list of advice generated from the evaluation of the rule
*/
public abstract List<Advice> evaluate(/*@ non_null @*/ AnnotatedString s,
/*@ non_null @*/ AnnotatedString original);
public abstract List<Advice> evaluate(/*@ non_null @*/ AnnotatedString s);

/**
* Creates a new rule
Expand Down
Loading

0 comments on commit 4676594

Please sign in to comment.