diff --git a/platform/lang-impl/src/com/intellij/psi/codeStyle/autodetect/ContinuationIndentDetector.java b/platform/lang-impl/src/com/intellij/psi/codeStyle/autodetect/ContinuationIndentDetector.java deleted file mode 100644 index 86f8d3ae2289..000000000000 --- a/platform/lang-impl/src/com/intellij/psi/codeStyle/autodetect/ContinuationIndentDetector.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright 2000-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.intellij.psi.codeStyle.autodetect; - -import com.intellij.util.containers.ContainerUtil; -import com.intellij.util.containers.Stack; -import com.intellij.util.text.CharArrayUtil; -import org.jetbrains.annotations.NotNull; - -class ContinuationIndentDetector { - private Stack myOpenedBrackets = ContainerUtil.newStack(); - - private final CharSequence myText; - private final int myLength; - private boolean myIncorrectBracketsOrder = false; - - private Character myOpeningStringLiteralDelimiter = null; - - public ContinuationIndentDetector(@NotNull CharSequence text) { - myText = text; - myLength = text.length(); - } - - public void feedLineStartingAt(int startOffset) { - if (myIncorrectBracketsOrder) return; - - int lineEndOffset = CharArrayUtil.indexOf(myText, "\n", startOffset, myLength); - if (lineEndOffset < 0) lineEndOffset = myLength; - - for (int i = startOffset; i < lineEndOffset; i++) { - final char currentChar = myText.charAt(i); - - if (isStringLiteralDelimiter(currentChar)) { - processStringLiteralDelimiter(currentChar); - } - else { - Bracket bracket = Bracket.forChar(currentChar); - if (bracket != null && myOpeningStringLiteralDelimiter == null) { - processBracket(bracket); - } - } - } - } - - private void processStringLiteralDelimiter(char delimiter) { - if (myOpeningStringLiteralDelimiter == null) { - myOpeningStringLiteralDelimiter = delimiter; - } - else if (myOpeningStringLiteralDelimiter.charValue() == delimiter) { - myOpeningStringLiteralDelimiter = null; - } - } - - private boolean isStringLiteralDelimiter(char currentChar) { - return currentChar == '\'' || currentChar == '\"'; - } - - private void processBracket(@NotNull Bracket bracket) { - if (bracket.isOpening()) { - myOpenedBrackets.add(bracket); - } - else { - if (!myOpenedBrackets.isEmpty() && bracket.isClosing(myOpenedBrackets.peek())) { - myOpenedBrackets.pop(); - } - else { - myIncorrectBracketsOrder = true; - } - } - } - - public boolean isContinuationIndent() { - if (myIncorrectBracketsOrder || myOpenedBrackets.isEmpty()) { - return false; - } - return myOpenedBrackets.peek() == Bracket.LPARENTH; - } - - - private enum Bracket { - LBRACE('{') { - @Override - boolean isOpening() { - return true; - } - }, - LPARENTH('(') { - @Override - boolean isOpening() { - return true; - } - }, - RBRACE('}') { - @Override - public boolean isClosing(Bracket bracket) { - return bracket == LBRACE; - } - }, - RPARENTH(')') { - @Override - public boolean isClosing(Bracket bracket) { - return bracket == LPARENTH; - } - }; - - private final char myChar; - - Bracket(char c) { - myChar = c; - } - - public boolean isClosing(Bracket bracket) { - return false; - } - - boolean isOpening() { - return false; - } - - static Bracket forChar(char c) { - for (Bracket bracket : Bracket.values()) { - if (bracket.myChar == c) return bracket; - } - return null; - } - } -} diff --git a/platform/lang-impl/src/com/intellij/psi/codeStyle/autodetect/LineIndentInfoBuilder.java b/platform/lang-impl/src/com/intellij/psi/codeStyle/autodetect/LineIndentInfoBuilder.java deleted file mode 100644 index 9a46f36535e2..000000000000 --- a/platform/lang-impl/src/com/intellij/psi/codeStyle/autodetect/LineIndentInfoBuilder.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright 2000-2014 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.intellij.psi.codeStyle.autodetect; - -import com.intellij.lang.Commenter; -import com.intellij.lang.Language; -import com.intellij.lang.LanguageCommenters; -import com.intellij.util.containers.ContainerUtil; -import com.intellij.util.text.CharArrayUtil; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.List; - -public class LineIndentInfoBuilder { - private static final int MAX_LINES_TO_PROCESS = 500; - - private final CharSequence myText; - private final int myLength; - private final Commenter myCommenter; - private ContinuationIndentDetector myContinuationIndentDetector; - - public LineIndentInfoBuilder(@NotNull CharSequence text, @Nullable Language language) { - myText = text; - myLength = text.length(); - myContinuationIndentDetector = new ContinuationIndentDetector(text); - myCommenter = language != null ? LanguageCommenters.INSTANCE.forLanguage(language) : null; - } - - @NotNull - public List build() { - List lineIndentInfos = ContainerUtil.newArrayList(); - - int lineStartOffset = 0; - int currentLine = 0; - - while (lineStartOffset < myText.length() && currentLine < MAX_LINES_TO_PROCESS) { - int lineEndOffset = getLineEndOffset(lineStartOffset); - int textStartOffset = CharArrayUtil.shiftForward(myText, lineStartOffset, lineEndOffset, " \t"); - - if (textStartOffset != lineEndOffset) { - lineIndentInfos.add(createInfoFromWhiteSpaceRange(lineStartOffset, textStartOffset)); - } else { - lineIndentInfos.add(LineIndentInfo.EMPTY_LINE); - } - - lineStartOffset = lineEndOffset + 1; - currentLine++; - } - - return lineIndentInfos; - } - - @NotNull - private LineIndentInfo createInfoFromWhiteSpaceRange(int lineStartOffset, int textStartOffset) { - if (startsWithComment(textStartOffset)) { - return LineIndentInfo.LINE_WITH_COMMENT; - } - else if (CharArrayUtil.indexOf(myText, "\t", lineStartOffset, textStartOffset) > 0) { - myContinuationIndentDetector.feedLineStartingAt(lineStartOffset); - return LineIndentInfo.LINE_WITH_TABS; - } - else { - boolean isContinuationIndent = myContinuationIndentDetector.isContinuationIndent(); - myContinuationIndentDetector.feedLineStartingAt(lineStartOffset); - return isContinuationIndent ? LineIndentInfo.LINE_WITH_CONTINUATION_INDENT - : LineIndentInfo.newNormalIndent(textStartOffset - lineStartOffset); - } - } - - private boolean startsWithComment(int textStartOffset) { - if (myText.charAt(textStartOffset) == '*' || startsWithLineComment(textStartOffset)) { - return true; - } - - return false; - } - - private boolean startsWithLineComment(int textStartOffset) { - if (myCommenter == null) return false; - - String lineCommentPrefix = myCommenter.getLineCommentPrefix(); - if (lineCommentPrefix != null && CharArrayUtil.regionMatches(myText, textStartOffset, lineCommentPrefix)) { - return true; - } - - return false; - } - - private int getLineEndOffset(int lineStartOffset) { - int lineEndOffset = CharArrayUtil.indexOf(myText, "\n", lineStartOffset, myLength); - if (lineEndOffset < 0) { - lineEndOffset = myText.length(); - } - return lineEndOffset; - } -} diff --git a/platform/lang-impl/testSources/com/intellij/psi/codeStyle/autodetect/ContinuationIndentDetectorTest.java b/platform/lang-impl/testSources/com/intellij/psi/codeStyle/autodetect/ContinuationIndentDetectorTest.java deleted file mode 100644 index 18444dfe4c0b..000000000000 --- a/platform/lang-impl/testSources/com/intellij/psi/codeStyle/autodetect/ContinuationIndentDetectorTest.java +++ /dev/null @@ -1,173 +0,0 @@ -/* - * Copyright 2000-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.intellij.psi.codeStyle.autodetect; - -import com.intellij.util.containers.ContainerUtil; -import junit.framework.TestCase; - -import java.util.List; -import java.util.Set; - - -public class ContinuationIndentDetectorTest extends TestCase { - private ContinuationIndentDetector myContinuationIndentDetector; - private List myLineStartOffsets; - - public void configure(String text) { - myContinuationIndentDetector = new ContinuationIndentDetector(text); - myLineStartOffsets = calculateLineStartOffsets(text); - } - - public void testContinuationIndentInsideParenth() { - configure( - "test(\n" + - " \"aaaaa\" + \n" + - " \"bbbbb\");\n" + - "int a = 2;" - ); - - doCheckLinesWithContinuationIndents(1, 2); - } - - public void testContinuationIndent_IfBracesWithinStringLiteral() { - configure( - "tester(\"{\",\n" + - " \"welcome\",\n" + - " \"my own\",\n" + - " \"string literal\")\n" - ); - - doCheckLinesWithContinuationIndents(1, 2, 3); - } - - public void testNoContinuationIndent_OnLineStartingWithRightParenth() { - configure( - "test(\n" + - " \"aaaaa\" + \n" + - " \"bbbbb\"\n" + - ");" - ); - - doCheckLinesWithContinuationIndents(1, 2, 3); - } - - public void testNoContinuationIndents_BetweenBraces() { - configure( - "import java.lang.Override;\n" + - "import java.lang.Runnable;\n" + - "\n" + - "class T {\n" + - " \n" + - " void test() {\n" + - " int a;\n" + - " int b;\n" + - " Runnable runnable = new Runnable() {\n" + - " @Override\n" + - " public void run() {\n" + - " System.out.println(\"AAA!\");\n" + - " }\n" + - " }\n" + - " }\n" + - " \n" + - "}" - ); - doCheckLinesWithContinuationIndents(); - } - - public void testMixedBraces() { - configure( - "import java.lang.Runnable;\n" + - "\n" + - "class R {\n" + - "\n" + - "\n" + - " void test() {\n" + - " kuu(new Runnable() {\n" + - " @Override\n" + - " public void run() {\n" + - " }\n" + - " },\n" + - " new Runnable() {\n" + - " @Override\n" + - " public void run() {\n" + - " }\n" + - " }\n" + - " );\n" + - " }\n" + - "\n" + - " void kuu(Runnable a, Runnable b) {\n" + - " }\n" + - "}" - ); - doCheckLinesWithContinuationIndents(11, 16); - } - - public void testClosingBracesAtLineStart() { - configure("class Test {\n" + - " public static void main(String[] args) {\n" + - " tree.put(\"Web Development\", Pair.create(PlatformImplIcons.WebDevelopment, Arrays.asList(\n" + - " \"HTML:HtmlTools\",\n" + - " \"com.intellij.css\",\n" + - " \"JavaScript:JavaScript,JSIntentionPowerPack\",\n" + - " \"com.jetbrains.restClient\"\n" + - " )));\n" + - " }\n" + - "}"); - doCheckLinesWithContinuationIndents(3, 4, 5, 6, 7); - } - - public void testInsideParenth() { - configure("@State(\n" + - " name = \"AndroidRemoteDataBaseManager\",\n" + - " storages = {\n" + - " @Storage(\n" + - " file = StoragePathMacros.APP_CONFIG + \"/androidRemoteDatabases.xml\"\n" + - " )}\n" + - ")\n" + - "class T {}"); - doCheckLinesWithContinuationIndents(1, 2, 4, 5, 6); - } - - private void doCheckLinesWithContinuationIndents(Integer... linesWithContinuationIndents) { - Set continuationLines = ContainerUtil.newHashSet(linesWithContinuationIndents); - - for (int currentLine = 0; currentLine < myLineStartOffsets.size(); currentLine++) { - Integer lineStartOffset = myLineStartOffsets.get(currentLine); - boolean isContinuation = myContinuationIndentDetector.isContinuationIndent(); - if (continuationLines.contains(currentLine)) { - assertTrue("Line " + currentLine + " should start with continuation indent", isContinuation); - } - else { - assertTrue("Line " + currentLine + " should not start with continuation indent" ,!isContinuation); - } - myContinuationIndentDetector.feedLineStartingAt(lineStartOffset); - } - } - - private List calculateLineStartOffsets(String text) { - List lineStartOffsets = ContainerUtil.newArrayList(); - lineStartOffsets.add(0); - - int lineStart = 0; - int lineFeed; - while ((lineFeed = text.indexOf('\n', lineStart)) > 0) { - lineStart = lineFeed + 1; - lineStartOffsets.add(lineStart); - } - - return lineStartOffsets; - } -} \ No newline at end of file