diff options
author | Tor Norbye <tnorbye@google.com> | 2012-06-18 22:33:39 -0700 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2012-06-22 14:42:30 -0700 |
commit | 5e290ab6060c3204be3a3eb6084db6f92c88adee (patch) | |
tree | 6edefecd33f674e453b070b38bb90813588f44e7 /common | |
parent | 960433ddfada5e246feccdc167a0af183f30bb0a (diff) | |
download | sdk-5e290ab6060c3204be3a3eb6084db6f92c88adee.zip sdk-5e290ab6060c3204be3a3eb6084db6f92c88adee.tar.gz sdk-5e290ab6060c3204be3a3eb6084db6f92c88adee.tar.bz2 |
Add typo detector
This changeset adds a new typo detector. There
are also some lint infrastructure fixes to better
handle positions within text nodes, and to allow
Eclipse lint quickfixes to supply multiple fixes
for a single issue (such as multiple misspelling
alternative replacements.)
Change-Id: Ie26f0bafc571e02ae09ff27a7f4b221fe0c2ea5b
Diffstat (limited to 'common')
-rw-r--r-- | common/src/com/android/util/PositionXmlParser.java | 72 | ||||
-rw-r--r-- | common/tests/src/com/android/util/PositionXmlParserTest.java | 6 |
2 files changed, 64 insertions, 14 deletions
diff --git a/common/src/com/android/util/PositionXmlParser.java b/common/src/com/android/util/PositionXmlParser.java index 6eee96f..08aeb0a 100644 --- a/common/src/com/android/util/PositionXmlParser.java +++ b/common/src/com/android/util/PositionXmlParser.java @@ -286,6 +286,25 @@ public class PositionXmlParser { */ @Nullable public Position getPosition(@NonNull Node node) { + return getPosition(node, -1, -1); + } + + /** + * Returns the position for the given node. This is the start position. The + * end position can be obtained via {@link Position#getEnd()}. A specific + * range within the node can be specified with the {@code start} and + * {@code end} parameters. + * + * @param node the node to look up position for + * @param start the relative offset within the node range to use as the + * starting position, inclusive, or -1 to not limit the range + * @param end the relative offset within the node range to use as the ending + * position, or -1 to not limit the range + * @return the position, or null if the node type is not supported for + * position info + */ + @Nullable + public Position getPosition(@NonNull Node node, int start, int end) { // Look up the position information stored while parsing for the given node. // Note however that we only store position information for elements (because // there is no SAX callback for individual attributes). @@ -301,6 +320,12 @@ public class PositionXmlParser { if (pos != null) { int startOffset = pos.getOffset(); int endOffset = pos.getEnd().getOffset(); + if (start != -1) { + startOffset += start; + if (end != -1) { + endOffset = start + end; + } + } // Find attribute in the text String contents = (String) node.getOwnerDocument().getUserData(CONTENT_KEY); @@ -369,26 +394,40 @@ public class PositionXmlParser { // Skip > offset++; + column++; - // Skip text whitespace prefix, if the text node contains non-whitespace - // characters String text = node.getNodeValue(); int textIndex = 0; int textLength = text.length(); int newLine = line; int newColumn = column; - for (; textIndex < text.length(); textIndex++) { - char t = text.charAt(textIndex); - if (t == '\n') { - newLine++; - newColumn = 0; - } else if (!Character.isWhitespace(t)) { - break; - } else { - newColumn++; + if (start != -1) { + textLength = Math.min(textLength, start); + for (; textIndex < textLength; textIndex++) { + char t = text.charAt(textIndex); + if (t == '\n') { + newLine++; + newColumn = 0; + } else { + newColumn++; + } + } + } else { + // Skip text whitespace prefix, if the text node contains + // non-whitespace characters + for (; textIndex < textLength; textIndex++) { + char t = text.charAt(textIndex); + if (t == '\n') { + newLine++; + newColumn = 0; + } else if (!Character.isWhitespace(t)) { + break; + } else { + newColumn++; + } } } - if (textIndex == textLength) { + if (textIndex == text.length()) { textIndex = 0; // Whitespace node } else { line = newLine; @@ -398,8 +437,13 @@ public class PositionXmlParser { Position attributePosition = createPosition(line, column, offset + textIndex); // Also set end range for retrieval in getLocation - attributePosition.setEnd(createPosition(line, column, - offset + textLength)); + if (end != -1) { + attributePosition.setEnd(createPosition(line, column, + offset + end)); + } else { + attributePosition.setEnd(createPosition(line, column, + offset + textLength)); + } return attributePosition; } else if (c == '"') { inAttribute = !inAttribute; diff --git a/common/tests/src/com/android/util/PositionXmlParserTest.java b/common/tests/src/com/android/util/PositionXmlParserTest.java index ba560a6..961d7d6 100644 --- a/common/tests/src/com/android/util/PositionXmlParserTest.java +++ b/common/tests/src/com/android/util/PositionXmlParserTest.java @@ -155,6 +155,12 @@ public class PositionXmlParserTest extends TestCase { assertEquals(11, start.getLine()); assertEquals(10, start.getColumn()); assertEquals(xml.indexOf("some text"), start.getOffset()); + + // Check attribute positions with text node offsets + start = parser.getPosition(text, 13, 15); + assertEquals(11, start.getLine()); + assertEquals(12, start.getColumn()); + assertEquals(xml.indexOf("me"), start.getOffset()); } public void testLineEndings() throws Exception { |