Skip to content

Commit

Permalink
ALS-728: Correction in offset convertion from Position
Browse files Browse the repository at this point in the history
  • Loading branch information
llibarona committed Mar 12, 2019
1 parent 6e4ac85 commit 1896b12
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
21 changes: 10 additions & 11 deletions als-common/shared/src/main/scala/common/dtoTypes/Position.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import amf.core.parser.{Position => AmfPosition}
* represented as a string, the `character` value represents the gap between the
* `character` and `character + 1`.
*/

case class Position(line: Int, column: Int) {
def offset(text: String): Int = {
def innerOffset(lines: List[String], currentLine: Int, currentOffset: Int): Int = lines match {
case Nil => currentOffset
case current :: Nil => Math.min(currentOffset + column, currentOffset + current.length - 1)
case current :: _ if currentLine == line => Math.min(currentOffset + column, currentOffset + current.length - 1)
case current :: rest => innerOffset(rest, currentLine + 1, currentOffset + current.length)
case Nil => currentOffset
case current :: Nil => Math.min(currentOffset + column, currentOffset + current.length)
case current :: _ if currentLine == line => Math.min(currentOffset + column, currentOffset + current.length)
case current :: rest => innerOffset(rest, currentLine + 1, currentOffset + current.length)
}

if (line < 0 || column < 0) -1 else innerOffset(text.linesWithSeparators.toList, 0, 0)
Expand All @@ -37,21 +36,21 @@ object Position {

def apply(offset: Int, text: String): Position = {
def toPosition(count: Int, line: Int, lines: List[String]): Position = lines match {
case Nil => Position(line, 0)
case currentLine :: Nil => Position(line, Math.min(currentLine.length, offset - count))
case Nil => Position(line, 0)
case currentLine :: Nil => Position(line, Math.min(currentLine.length, offset - count))
case currentLine :: _ if offset - count < currentLine.length => Position(line, offset - count)
case current :: rest => toPosition(count + current.length, line + 1, rest)
case current :: rest => toPosition(count + current.length, line + 1, rest)
}

offset match {
case value if value < 0 => Position0
case _ => toPosition(0, 0, text.linesWithSeparators.toList)
case _ => toPosition(0, 0, text.linesWithSeparators.toList)
}
}

def min(first: Position, second: Position): Position = if(second < first) second else first
def min(first: Position, second: Position): Position = if (second < first) second else first

def max(first: Position, second: Position): Position = if(second > first) second else first
def max(first: Position, second: Position): Position = if (second > first) second else first
}

object Position0 extends Position(0, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class LanguageServerImpl(val connection: ServerConnection,
server.registerModule(new FindDeclarationModule())
serverCapabilities.setDefinitionProvider(true)

// server.registerModule(new RenameModule())
// serverCapabilities.setRenameProvider(true)
server.registerModule(new RenameModule())
serverCapabilities.setRenameProvider(true)

server
.enableModule(ASTManagerModule.moduleId)
Expand Down

0 comments on commit 1896b12

Please sign in to comment.