Search in sources :

Example 1 with TabNannyDocIterator

use of org.python.pydev.core.docutils.TabNannyDocIterator in project Pydev by fabioz.

the class TabNanny method analyzeDoc.

/**
 * Analyze the doc for mixed tabs and indents with the wrong number of chars.
 * @param monitor
 *
 * @return a list with the error messages to be shown to the user.
 */
public static List<IMessage> analyzeDoc(IDocument doc, IAnalysisPreferences analysisPrefs, String moduleName, IIndentPrefs indentPrefs, IProgressMonitor monitor) {
    ArrayList<IMessage> ret = new ArrayList<IMessage>();
    // don't even try to gather indentation errors if they should be ignored.
    if (analysisPrefs.getSeverityForType(IAnalysisPreferences.TYPE_INDENTATION_PROBLEM) < IMarker.SEVERITY_INFO) {
        return ret;
    }
    List<IndentInfo> foundTabs = new ArrayList<IndentInfo>();
    List<IndentInfo> foundSpaces = new ArrayList<IndentInfo>();
    TabNannyDocIterator it;
    try {
        it = new TabNannyDocIterator(doc);
    } catch (BadLocationException e) {
        return ret;
    }
    while (it.hasNext()) {
        IndentInfo indentation;
        try {
            indentation = it.next();
        } catch (BadLocationException e) {
            return ret;
        }
        // it can actually be in both (if we have spaces and tabs in the same indent line).
        if (indentation.indent.indexOf('\t') != -1) {
            foundTabs.add(indentation);
        }
        if (indentation.indent.indexOf(' ') != -1) {
            foundSpaces.add(indentation);
        }
        if (monitor.isCanceled()) {
            return ret;
        }
    }
    int spacesFoundSize = foundSpaces.size();
    int tabsFoundSize = foundTabs.size();
    if (spacesFoundSize == 0 && tabsFoundSize == 0) {
        // nothing to do here... (no indents available)
        return ret;
    }
    // let's discover whether we should mark the tabs found as errors or the spaces found...
    boolean markTabsAsError;
    // if we found the same number of indents for tabs and spaces, let's use the user-prefs to decide what to do
    if (spacesFoundSize == tabsFoundSize) {
        // ok, we have both, spaces and tabs... let's see what the user actually wants
        markTabsAsError = indentPrefs.getUseSpaces(false);
    } else if (tabsFoundSize > spacesFoundSize) {
        // let's see what appears more in the file (and mark the other as error).
        markTabsAsError = false;
    } else {
        markTabsAsError = true;
    }
    List<IndentInfo> errorsAre;
    List<IndentInfo> validsAre;
    String errorMsg;
    char errorChar;
    if (markTabsAsError) {
        validsAre = foundSpaces;
        errorsAre = foundTabs;
        errorMsg = "Mixed Indentation: Tab found";
        errorChar = '\t';
        createBadIndentForSpacesMessages(doc, analysisPrefs, indentPrefs, ret, validsAre, monitor);
    } else {
        validsAre = foundTabs;
        errorsAre = foundSpaces;
        errorMsg = "Mixed Indentation: Spaces found";
        errorChar = ' ';
    }
    createMixedErrorMessages(doc, analysisPrefs, ret, errorsAre, errorMsg, errorChar, monitor);
    return ret;
}
Also used : IndentInfo(org.python.pydev.core.docutils.TabNannyDocIterator.IndentInfo) TabNannyDocIterator(org.python.pydev.core.docutils.TabNannyDocIterator) IMessage(org.python.pydev.ast.analysis.messages.IMessage) ArrayList(java.util.ArrayList) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with TabNannyDocIterator

use of org.python.pydev.core.docutils.TabNannyDocIterator in project Pydev by fabioz.

the class ScopesParser method createScopes.

@Override
public Scopes createScopes(IDocument doc) {
    this.scopes = new Scopes();
    this.doc = doc;
    this.lineOffsetToIndent = new TreeMap<Integer, Integer>();
    try {
        TabNannyDocIterator nannyDocIterator = new TabNannyDocIterator(doc, true, false);
        while (nannyDocIterator.hasNext()) {
            IndentInfo next = nannyDocIterator.next();
            this.lineOffsetToIndent.put(next.startOffset, next.indent.length());
        }
    } catch (BadLocationException e1) {
        throw new RuntimeException(e1);
    }
    try {
        return this.createScopes();
    } catch (SyntaxErrorException e) {
        throw new RuntimeException(e);
    }
}
Also used : IndentInfo(org.python.pydev.core.docutils.TabNannyDocIterator.IndentInfo) TabNannyDocIterator(org.python.pydev.core.docutils.TabNannyDocIterator) Scopes(org.python.pydev.shared_core.parsing.Scopes) SyntaxErrorException(org.python.pydev.core.docutils.SyntaxErrorException) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 3 with TabNannyDocIterator

use of org.python.pydev.core.docutils.TabNannyDocIterator in project Pydev by fabioz.

the class TabNannyIteratorTest method testIterator6.

public void testIterator6() throws Exception {
    String str = // escape is in comment... (so, it's not considered the same line)
    "    #comment   what's happening\\\n" + "    pass";
    Document d = new Document(str);
    TabNannyDocIterator it = new TabNannyDocIterator(d);
    assertEquals("    ", it.next().indent);
    assertEquals("    ", it.next().indent);
    assertTrue(!it.hasNext());
}
Also used : TabNannyDocIterator(org.python.pydev.core.docutils.TabNannyDocIterator) Document(org.eclipse.jface.text.Document)

Example 4 with TabNannyDocIterator

use of org.python.pydev.core.docutils.TabNannyDocIterator in project Pydev by fabioz.

the class TabNannyIteratorTest method testIterator6c.

public void testIterator6c() throws Exception {
    String str = "    #comment   what's happening\r\n" + "    #comment   what's happening2\r\n" + "    pass\r\n";
    Document d = new Document(str);
    TabNannyDocIterator it = new TabNannyDocIterator(d, true, false);
    assertEquals("    ", it.next().indent);
    assertEquals("    ", it.next().indent);
    assertEquals("    ", it.next().indent);
    assertTrue(!it.hasNext());
}
Also used : TabNannyDocIterator(org.python.pydev.core.docutils.TabNannyDocIterator) Document(org.eclipse.jface.text.Document)

Example 5 with TabNannyDocIterator

use of org.python.pydev.core.docutils.TabNannyDocIterator in project Pydev by fabioz.

the class TabNannyIteratorTest method testIterator3.

public void testIterator3() throws Exception {
    String str = "" + "d\n" + "    '''\r" + "    '''\r" + "\t" + "";
    Document d = new Document(str);
    TabNannyDocIterator it = new TabNannyDocIterator(d);
    assertEquals("    ", it.next().indent);
    assertEquals("\t", it.next().indent);
    // no indentations here...
    assertTrue(!it.hasNext());
}
Also used : TabNannyDocIterator(org.python.pydev.core.docutils.TabNannyDocIterator) Document(org.eclipse.jface.text.Document)

Aggregations

TabNannyDocIterator (org.python.pydev.core.docutils.TabNannyDocIterator)30 Document (org.eclipse.jface.text.Document)28 BadLocationException (org.eclipse.jface.text.BadLocationException)2 IndentInfo (org.python.pydev.core.docutils.TabNannyDocIterator.IndentInfo)2 ArrayList (java.util.ArrayList)1 IMessage (org.python.pydev.ast.analysis.messages.IMessage)1 SyntaxErrorException (org.python.pydev.core.docutils.SyntaxErrorException)1 Scopes (org.python.pydev.shared_core.parsing.Scopes)1