Add utility function for getting text with context
This commit is contained in:
@@ -27,6 +27,7 @@ import org.jetbrains.annotations.TestOnly;
|
||||
import org.jetbrains.jet.lexer.JetKeywordToken;
|
||||
import org.jetbrains.jet.lexer.JetToken;
|
||||
import org.jetbrains.jet.lexer.JetTokens;
|
||||
import org.jetbrains.jet.utils.strings.StringsPackage;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -368,12 +369,8 @@ import static org.jetbrains.jet.lexer.JetTokens.*;
|
||||
currentPosition.rollbackTo();
|
||||
|
||||
if (endOffset - beginOffset > LOOK_AHEAD_SIZE_WARN && !ApplicationManager.getApplication().isHeadlessEnvironment()) {
|
||||
String text = new StringBuilder(myBuilder.getOriginalText())
|
||||
.insert(endOffset, "<~~!END!~~>")
|
||||
.insert(beginOffset, "<~~!BEGIN!~~>")
|
||||
.toString();
|
||||
|
||||
LOGGER.warn("Suspicious big range: \n" + text);
|
||||
String rangeWithContext = StringsPackage.substringWithContext(myBuilder.getOriginalText(), beginOffset, endOffset, 100);
|
||||
LOGGER.warn("Suspicious big range: \n" + rangeWithContext);
|
||||
}
|
||||
|
||||
return pattern.result();
|
||||
@@ -497,16 +494,6 @@ import static org.jetbrains.jet.lexer.JetTokens.*;
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
@TestOnly
|
||||
public String currentContext() {
|
||||
String marker = "~!!!~";
|
||||
int range = 50;
|
||||
|
||||
CharSequence text = myBuilder.getOriginalText();
|
||||
|
||||
int start = Math.max(0, myBuilder.getCurrentOffset() - range);
|
||||
int end = Math.min(text.length() - 1, myBuilder.getCurrentOffset() + 50 + marker.length());
|
||||
|
||||
return new StringBuilder(text)
|
||||
.insert(myBuilder.getCurrentOffset(), marker)
|
||||
.substring(start, end);
|
||||
return StringsPackage.substringWithContext(myBuilder.getOriginalText(), myBuilder.getCurrentOffset(), myBuilder.getCurrentOffset(), 20);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2010-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 org.jetbrains.jet.utils.strings
|
||||
|
||||
import com.intellij.openapi.util.text.StringUtil
|
||||
|
||||
private val CARET_MARKER = "<~!!~>"
|
||||
private val BEGIN_MARKER = "<~BEGIN~>"
|
||||
private val END_MARKER = "<~END~>"
|
||||
|
||||
public fun CharSequence.substringWithContext(beginIndex: Int, endIndex: Int, range: Int): String {
|
||||
val start = Math.max(0, beginIndex - range)
|
||||
val end = Math.min(this.length(), endIndex + range)
|
||||
|
||||
val notFromBegin = start != 0
|
||||
val notToEnd = end != this.length()
|
||||
|
||||
val updatedStart = beginIndex - start
|
||||
val updatedEnd = endIndex - start
|
||||
|
||||
return StringBuilder(this.toString().substring(start, end))
|
||||
.insert(updatedEnd, if (updatedEnd == updatedStart) CARET_MARKER else END_MARKER)
|
||||
.insert(updatedStart, if (updatedEnd == updatedStart) "" else BEGIN_MARKER)
|
||||
.insert(0, if (notFromBegin) "<~...${position(this, start)}~>" else "")
|
||||
.append(if (notToEnd) "<~${position(this, end)}...~>" else "").toString()
|
||||
}
|
||||
|
||||
private fun position(str: CharSequence, offset: Int): String {
|
||||
val line = StringUtil.offsetToLineNumber(str, offset) + 1
|
||||
return "(line: $line)"
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user