Skip to content

Commit

Permalink
Merge pull request #1721 from kaliGo-Li/Resolving-NoSuchElementExcept…
Browse files Browse the repository at this point in the history
…ion-for-LexerUtils.scala

Resolving NoSuchElementException for LexerUtils.scala
  • Loading branch information
hellozepp authored Mar 25, 2022
2 parents 844f2d7 + 5c8e233 commit d8230cf
Showing 1 changed file with 31 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import tech.mlsql.autosuggest.dsl.{MLSQLTokenTypeWrapper, TokenTypeWrapper}
import tech.mlsql.autosuggest.{AutoSuggestContext, TokenPos, TokenPosType}

import scala.collection.JavaConverters._
import scala.collection.mutable.ListBuffer

/**
* 1/6/2020 WilliamZhu(allwefantasy@gmail.com)
Expand Down Expand Up @@ -59,27 +60,30 @@ object LexerUtils {
* load[cursor] in token
*/

if (tokens.size == 0) {
if (tokens.isEmpty) {
return TokenPos(-1, TokenPosType.NEXT, -1)
}

val _lastToken: Token = tokens.last
var _lastTokenIndex = 0
var _lastLineHeadToken: Token = _lastToken
var _lastLineHeadTokenNum: Int = -1
var _lastLineHeadTokenIndex = 0
val oneLineTokens = tokens.zipWithIndex.filter { case (token, index) =>
_lastTokenIndex = index
if (_lastLineHeadTokenNum != token.getLine) {
_lastLineHeadTokenIndex = index
_lastLineHeadToken = token
_lastLineHeadTokenNum = token.getLine
}
token.getLine == lineNum
}

val firstToken = oneLineTokens.headOption match {
case Some(head) => head
case None =>
tokens.zipWithIndex.filter { case (token, index) =>
token.getLine == lineNum - 1
}.head
case None => (_lastLineHeadToken, _lastLineHeadTokenIndex)
}
val lastToken = oneLineTokens.lastOption match {
case Some(last) => last
case None =>
tokens.zipWithIndex.filter { case (token, index) =>
token.getLine == lineNum + 1
}.last
case None => (_lastToken, _lastTokenIndex)
}

if (colNum < firstToken._1.getCharPositionInLine) {
Expand Down Expand Up @@ -134,27 +138,30 @@ object LexerUtils {
* load[cursor] in token
*/

if (tokens.size == 0) {
if (tokens.isEmpty) {
return TokenPos(-1, TokenPosType.NEXT, -1)
}

val _lastToken: Token = tokens.last
var _lastTokenIndex = 0
var _lastLineHeadToken: Token = _lastToken
var _lastLineHeadTokenNum: Int = -1
var _lastLineHeadTokenIndex = 0
val oneLineTokens = tokens.zipWithIndex.filter { case (token, index) =>
_lastTokenIndex = index
if (_lastLineHeadTokenNum != token.getLine) {
_lastLineHeadTokenIndex = index
_lastLineHeadToken = token
_lastLineHeadTokenNum = token.getLine
}
token.getLine == lineNum
}

val firstToken = oneLineTokens.headOption match {
case Some(head) => head
case None =>
tokens.zipWithIndex.filter { case (token, index) =>
token.getLine == lineNum - 1
}.head
case None => (_lastLineHeadToken, _lastLineHeadTokenIndex)
}
val lastToken = oneLineTokens.lastOption match {
case Some(last) => last
case None =>
tokens.zipWithIndex.filter { case (token, index) =>
token.getLine == lineNum + 1
}.last
case None => (_lastToken, _lastTokenIndex)
}

if (colNum < firstToken._1.getCharPositionInLine) {
Expand Down Expand Up @@ -195,6 +202,7 @@ object LexerUtils {
}.filterNot(_.pos == -2).head
}


def isInWhereContext(tokens: List[Token], tokenPos: Int): Boolean = {
if (tokenPos < 1) return false
var wherePos = -1
Expand Down Expand Up @@ -267,3 +275,4 @@ object LexerUtils {
}.filter(_ != null)
}
}

0 comments on commit d8230cf

Please sign in to comment.