From db488ad295f2a8f05dc53ae3d7a880c0ecfaac69 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Fri, 19 Dec 2014 16:42:48 +0300 Subject: [PATCH] Add utility function for getting text with context --- .../jet/lang/parsing/AbstractJetParsing.java | 21 ++------- .../src/org/jetbrains/jet/utils/strings.kt | 47 +++++++++++++++++++ 2 files changed, 51 insertions(+), 17 deletions(-) create mode 100644 compiler/util/src/org/jetbrains/jet/utils/strings.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/parsing/AbstractJetParsing.java b/compiler/frontend/src/org/jetbrains/jet/lang/parsing/AbstractJetParsing.java index 9b90e5657a7..04a01503fc8 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/parsing/AbstractJetParsing.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/parsing/AbstractJetParsing.java @@ -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); } } diff --git a/compiler/util/src/org/jetbrains/jet/utils/strings.kt b/compiler/util/src/org/jetbrains/jet/utils/strings.kt new file mode 100644 index 00000000000..d83315953c3 --- /dev/null +++ b/compiler/util/src/org/jetbrains/jet/utils/strings.kt @@ -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)" +} + +