From 7813d877afebffcedeb8cd80c05ff3d4e47fd3b8 Mon Sep 17 00:00:00 2001 From: Katarzyna Marek Date: Fri, 15 Sep 2023 16:28:09 +0200 Subject: [PATCH] bugfix: handle CR --- .../SyntheticsDecorationProvider.scala | 5 ++--- .../scala/meta/internal/metals/Buffers.scala | 3 +-- .../internal/metals/DefinitionProvider.scala | 2 +- .../scala/meta/internal/metals/Indexer.scala | 2 +- .../internal/metals/MetalsEnrichments.scala | 4 ++-- .../meta/internal/metals/PackageProvider.scala | 2 +- .../formatting/OnTypeFormattingProvider.scala | 3 +-- .../formatting/RangeFormattingProvider.scala | 3 +-- .../internal/parsing/FoldingRangeProvider.scala | 5 ++--- .../scala/meta/internal/parsing/Trees.scala | 2 +- .../mtags/ScalametaCommonEnrichments.scala | 17 ++++++++++++++++- .../meta/internal/mtags/SymbolIndexBucket.scala | 3 +-- .../main/scala/tests/TestingSymbolSearch.scala | 4 ++-- .../scala/tests/TestingWorkspaceSearch.scala | 5 ++--- tests/unit/src/main/scala/tests/Library.scala | 7 +++++++ .../test/scala/tests/ToplevelLibrarySuite.scala | 3 ++- 16 files changed, 43 insertions(+), 27 deletions(-) diff --git a/metals/src/main/scala/scala/meta/internal/decorations/SyntheticsDecorationProvider.scala b/metals/src/main/scala/scala/meta/internal/decorations/SyntheticsDecorationProvider.scala index feed18b1dad..8d8c609d1e3 100644 --- a/metals/src/main/scala/scala/meta/internal/decorations/SyntheticsDecorationProvider.scala +++ b/metals/src/main/scala/scala/meta/internal/decorations/SyntheticsDecorationProvider.scala @@ -9,7 +9,6 @@ import scala.concurrent.Future import scala.util.Try import scala.{meta => m} -import scala.meta.inputs.Input import scala.meta.internal.metals.Buffers import scala.meta.internal.metals.ClientCommands import scala.meta.internal.metals.ClientConfiguration @@ -551,8 +550,8 @@ final class SyntheticsDecorationProvider( val uri = path.toURI.toString val typeDecorations = for { tree <- trees.get(path).toIterable - textDocumentInput = Input.VirtualFile(uri, textDocument.text) - treeInput = Input.VirtualFile(uri, tree.pos.input.text) + textDocumentInput = VirtualFile(uri, textDocument.text) + treeInput = VirtualFile(uri, tree.pos.input.text) semanticDbToTreeEdit = TokenEditDistance( textDocumentInput, treeInput, diff --git a/metals/src/main/scala/scala/meta/internal/metals/Buffers.scala b/metals/src/main/scala/scala/meta/internal/metals/Buffers.scala index 4ac56b33a5f..e18db934fec 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/Buffers.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/Buffers.scala @@ -2,7 +2,6 @@ package scala.meta.internal.metals import scala.collection.concurrent.TrieMap -import scala.meta.inputs.Input import scala.meta.internal.metals.MetalsEnrichments._ import scala.meta.internal.parsing.TokenEditDistance import scala.meta.internal.parsing.Trees @@ -26,7 +25,7 @@ case class Buffers( trees: Trees, ): TokenEditDistance = { val bufferInput = source.toInputFromBuffers(this) - val snapshotInput = Input.VirtualFile(bufferInput.path, snapshot) + val snapshotInput = VirtualFile(bufferInput.path, snapshot) TokenEditDistance(snapshotInput, bufferInput, trees).getOrElse( TokenEditDistance.NoMatch ) diff --git a/metals/src/main/scala/scala/meta/internal/metals/DefinitionProvider.scala b/metals/src/main/scala/scala/meta/internal/metals/DefinitionProvider.scala index 9071c288af2..be1b89ae482 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/DefinitionProvider.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/DefinitionProvider.scala @@ -136,7 +136,7 @@ final class DefinitionProvider( val defResult = for { sourceText <- buffers.get(path) - virtualFile = Input.VirtualFile(path.toURI.toString(), sourceText) + virtualFile = VirtualFile(path.toURI.toString(), sourceText) metaPos <- pos.toMeta(virtualFile) tokens <- trees.tokenized(virtualFile).toOption ident <- tokens.collectFirst { diff --git a/metals/src/main/scala/scala/meta/internal/metals/Indexer.scala b/metals/src/main/scala/scala/meta/internal/metals/Indexer.scala index cacdd666ec9..6e53f346db8 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/Indexer.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/Indexer.scala @@ -276,7 +276,7 @@ final case class Indexer( val updatedContent = sourceItem.getTopWrapper + content + sourceItem.getBottomWrapper ( - Input.VirtualFile( + VirtualFile( generatedPath.toNIO.toString .stripSuffix(".scala") + ".sc.scala", updatedContent, diff --git a/metals/src/main/scala/scala/meta/internal/metals/MetalsEnrichments.scala b/metals/src/main/scala/scala/meta/internal/metals/MetalsEnrichments.scala index ce5d461643f..f18d0547233 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/MetalsEnrichments.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/MetalsEnrichments.scala @@ -294,7 +294,7 @@ object MetalsEnrichments def toUriInput: Input.VirtualFile = { val uri = path.toAbsolutePath.toUri.toString val text = new String(Files.readAllBytes(path), StandardCharsets.UTF_8) - Input.VirtualFile(uri, text) + VirtualFile(uri, text) } def isSemanticdb: Boolean = @@ -489,7 +489,7 @@ object MetalsEnrichments */ def toInputFromBuffers(buffers: Buffers): m.Input.VirtualFile = { buffers.get(path) match { - case Some(text) => Input.VirtualFile(path.toURI.toString(), text) + case Some(text) => VirtualFile(path.toURI.toString(), text) case None => path.toInput } } diff --git a/metals/src/main/scala/scala/meta/internal/metals/PackageProvider.scala b/metals/src/main/scala/scala/meta/internal/metals/PackageProvider.scala index d9d6f2490eb..cacc5e760e1 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/PackageProvider.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/PackageProvider.scala @@ -659,7 +659,7 @@ class PackageProvider( val result = for { content <- path.content() } yield { - val input = Input.VirtualFile(filename, content) + val input = VirtualFile(filename, content) def isPackageObjectLike(symbol: String) = Set("package", filenamePart ++ "$package").contains(symbol) diff --git a/metals/src/main/scala/scala/meta/internal/metals/formatting/OnTypeFormattingProvider.scala b/metals/src/main/scala/scala/meta/internal/metals/formatting/OnTypeFormattingProvider.scala index 04b4dca3620..d80f39ad4d6 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/formatting/OnTypeFormattingProvider.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/formatting/OnTypeFormattingProvider.scala @@ -1,6 +1,5 @@ package scala.meta.internal.metals.formatting -import scala.meta.inputs.Input import scala.meta.internal.metals.Buffers import scala.meta.internal.metals.MetalsEnrichments._ import scala.meta.internal.metals.UserConfiguration @@ -51,7 +50,7 @@ class OnTypeFormattingProvider( val edits = for { sourceText <- buffers.get(path) - virtualFile = Input.VirtualFile(path.toURI.toString(), sourceText) + virtualFile = VirtualFile(path.toURI.toString(), sourceText) startPos <- range.getStart.toMeta(virtualFile) endPos <- range.getEnd.toMeta(virtualFile) } yield { diff --git a/metals/src/main/scala/scala/meta/internal/metals/formatting/RangeFormattingProvider.scala b/metals/src/main/scala/scala/meta/internal/metals/formatting/RangeFormattingProvider.scala index 2b7417aa014..33f8a9b46f4 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/formatting/RangeFormattingProvider.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/formatting/RangeFormattingProvider.scala @@ -1,6 +1,5 @@ package scala.meta.internal.metals.formatting -import scala.meta.inputs.Input import scala.meta.internal.metals.Buffers import scala.meta.internal.metals.MetalsEnrichments._ import scala.meta.internal.metals.UserConfiguration @@ -47,7 +46,7 @@ class RangeFormattingProvider( val formattingOptions = params.getOptions val edits = for { sourceText <- buffers.get(path) - virtualFile = Input.VirtualFile(path.toURI.toString(), sourceText) + virtualFile = VirtualFile(path.toURI.toString(), sourceText) startPos <- range.getStart.toMeta(virtualFile) endPos <- range.getEnd.toMeta(virtualFile) } yield { diff --git a/metals/src/main/scala/scala/meta/internal/parsing/FoldingRangeProvider.scala b/metals/src/main/scala/scala/meta/internal/parsing/FoldingRangeProvider.scala index 762e11cb607..8dfd49d4917 100644 --- a/metals/src/main/scala/scala/meta/internal/parsing/FoldingRangeProvider.scala +++ b/metals/src/main/scala/scala/meta/internal/parsing/FoldingRangeProvider.scala @@ -3,7 +3,6 @@ package scala.meta.internal.parsing import java.util import java.util.Collections -import scala.meta.inputs.Input import scala.meta.inputs.Position import scala.meta.internal.metals.Buffers import scala.meta.internal.metals.MetalsEnrichments._ @@ -26,9 +25,9 @@ final class FoldingRangeProvider( if filePath.isScala tree <- trees.get(filePath) } yield { - val revised = Input.VirtualFile(filePath.toURI.toString(), code) + val revised = VirtualFile(filePath.toURI.toString(), code) val fromTree = - Input.VirtualFile(filePath.toURI.toString(), tree.pos.input.text) + VirtualFile(filePath.toURI.toString(), tree.pos.input.text) val distance = TokenEditDistance(fromTree, revised, trees).getOrElse( TokenEditDistance.NoMatch ) diff --git a/metals/src/main/scala/scala/meta/internal/parsing/Trees.scala b/metals/src/main/scala/scala/meta/internal/parsing/Trees.scala index c22b2926e71..42a8174dfdb 100644 --- a/metals/src/main/scala/scala/meta/internal/parsing/Trees.scala +++ b/metals/src/main/scala/scala/meta/internal/parsing/Trees.scala @@ -131,7 +131,7 @@ final class Trees( } yield try { val skipFistShebang = if (text.startsWith("#!")) text.replaceFirst("#!", "//") else text - val input = Input.VirtualFile(path.toURI.toString(), skipFistShebang) + val input = VirtualFile(path.toURI.toString(), skipFistShebang) if (path.isAmmoniteScript || path.isMill) { val ammoniteInput = Input.Ammonite(input) dialect(ammoniteInput).parse(Parse.parseAmmonite) diff --git a/mtags/src/main/scala/scala/meta/internal/mtags/ScalametaCommonEnrichments.scala b/mtags/src/main/scala/scala/meta/internal/mtags/ScalametaCommonEnrichments.scala index 29968293341..df1d96be77b 100644 --- a/mtags/src/main/scala/scala/meta/internal/mtags/ScalametaCommonEnrichments.scala +++ b/mtags/src/main/scala/scala/meta/internal/mtags/ScalametaCommonEnrichments.scala @@ -377,7 +377,7 @@ trait ScalametaCommonEnrichments extends CommonMtagsEnrichments { def toInput: Input.VirtualFile = { val text = FileIO.slurp(path, StandardCharsets.UTF_8) val file = path.toURI.toString() - Input.VirtualFile(file, text) + VirtualFile(file, text) } def jarPath: Option[AbsolutePath] = { @@ -526,4 +526,19 @@ trait ScalametaCommonEnrichments extends CommonMtagsEnrichments { } + object VirtualFile { + def apply(path: String, value: String): Input.VirtualFile = { + val buffer = new StringBuilder() + var isLastCR = false + value.foreach { char => + val isCR = char == '\r' + def CRSubstitute = if (char == '\n') ' ' else '\n' + if (isLastCR) buffer.append(CRSubstitute) + if (!isCR) buffer.append(char) + isLastCR = isCR + } + Input.VirtualFile(path, buffer.result()) + } + } + } diff --git a/mtags/src/main/scala/scala/meta/internal/mtags/SymbolIndexBucket.scala b/mtags/src/main/scala/scala/meta/internal/mtags/SymbolIndexBucket.scala index ce0431b38a6..c071e6269ae 100644 --- a/mtags/src/main/scala/scala/meta/internal/mtags/SymbolIndexBucket.scala +++ b/mtags/src/main/scala/scala/meta/internal/mtags/SymbolIndexBucket.scala @@ -11,7 +11,6 @@ import scala.util.Properties import scala.util.control.NonFatal import scala.meta.Dialect -import scala.meta.inputs.Input import scala.meta.internal.io.FileIO import scala.meta.internal.io.PathIO import scala.meta.internal.mtags.ScalametaCommonEnrichments._ @@ -129,7 +128,7 @@ class SymbolIndexBucket( dialect: Dialect ): List[String] = { val text = FileIO.slurp(source, StandardCharsets.UTF_8) - val input = Input.VirtualFile(uri, text) + val input = VirtualFile(uri, text) val sourceToplevels = mtags.toplevels(input, dialect) if (source.isAmmoniteScript) sourceToplevels diff --git a/tests/mtest/src/main/scala/tests/TestingSymbolSearch.scala b/tests/mtest/src/main/scala/tests/TestingSymbolSearch.scala index 9619e0aa06a..017ec8e4071 100644 --- a/tests/mtest/src/main/scala/tests/TestingSymbolSearch.scala +++ b/tests/mtest/src/main/scala/tests/TestingSymbolSearch.scala @@ -5,7 +5,6 @@ import java.nio.file.Files import java.util.Optional import java.{util => ju} -import scala.meta.inputs.Input import scala.meta.internal.metals.ClasspathSearch import scala.meta.internal.metals.Docstrings import scala.meta.internal.metals.EmptyReportContext @@ -15,6 +14,7 @@ import scala.meta.internal.metals.WorkspaceSymbolQuery import scala.meta.internal.mtags.GlobalSymbolIndex import scala.meta.internal.mtags.Mtags import scala.meta.internal.mtags.OnDemandSymbolIndex +import scala.meta.internal.mtags.ScalametaCommonEnrichments._ import scala.meta.internal.mtags.Symbol import scala.meta.internal.{semanticdb => s} import scala.meta.pc.ParentSymbols @@ -73,7 +73,7 @@ class TestingSymbolSearch( import scala.collection.JavaConverters._ val filename = value.path.toNIO.getFileName().toString() val content = new String(Files.readAllBytes(value.path.toNIO)) - val input = Input.VirtualFile( + val input = VirtualFile( filename, content, ) diff --git a/tests/mtest/src/main/scala/tests/TestingWorkspaceSearch.scala b/tests/mtest/src/main/scala/tests/TestingWorkspaceSearch.scala index 577c6980fad..860c565cbb4 100644 --- a/tests/mtest/src/main/scala/tests/TestingWorkspaceSearch.scala +++ b/tests/mtest/src/main/scala/tests/TestingWorkspaceSearch.scala @@ -5,13 +5,12 @@ import java.nio.file.Paths import scala.collection.mutable import scala.meta.Dialect -import scala.meta.inputs.Input import scala.meta.internal.metals.EmptyReportContext import scala.meta.internal.metals.ReportContext import scala.meta.internal.metals.SemanticdbDefinition import scala.meta.internal.metals.WorkspaceSymbolInformation import scala.meta.internal.metals.WorkspaceSymbolQuery -import scala.meta.internal.mtags.ScalametaCommonEnrichments.XtensionWorkspaceSymbolQuery +import scala.meta.internal.mtags.ScalametaCommonEnrichments._ import scala.meta.pc.SymbolSearchVisitor object TestingWorkspaceSearch { @@ -32,7 +31,7 @@ class TestingWorkspaceSearch(implicit rc: ReportContext = EmptyReportContext) { (path, (text, dialect)) <- inputs } { SemanticdbDefinition.foreach( - Input.VirtualFile(path, text), + VirtualFile(path, text), dialect, includeMembers = true, ) { defn => diff --git a/tests/unit/src/main/scala/tests/Library.scala b/tests/unit/src/main/scala/tests/Library.scala index a284ec4ba6d..f030f12e154 100644 --- a/tests/unit/src/main/scala/tests/Library.scala +++ b/tests/unit/src/main/scala/tests/Library.scala @@ -59,6 +59,13 @@ object Library { ).sources.entries .filter(_.toString.endsWith("bindings-rxjava-2.0.0-sources.jar")) + def msal4jSources: List[AbsolutePath] = + fetchSources( + "azure", + List(Dependency.of("com.microsoft.azure", "msal4j", "1.13.8")), + ).sources.entries + .filter(_.toString().endsWith("msal4j-1.13.8-sources.jar")) + def allScala2: List[Library] = { import mtags.BuildInfo.scalaCompilerVersion diff --git a/tests/unit/src/test/scala/tests/ToplevelLibrarySuite.scala b/tests/unit/src/test/scala/tests/ToplevelLibrarySuite.scala index f5c0786c22f..e3071e8f972 100644 --- a/tests/unit/src/test/scala/tests/ToplevelLibrarySuite.scala +++ b/tests/unit/src/test/scala/tests/ToplevelLibrarySuite.scala @@ -23,7 +23,8 @@ class ToplevelLibrarySuite extends BaseSuite { "/dotty/tools/dotc/transform/patmat/Space.scala", ) - val javaTestClasspath: List[AbsolutePath] = Library.damlrxjavaSources + val javaTestClasspath: List[AbsolutePath] = + Library.damlrxjavaSources ++ Library.msal4jSources scala2TestClasspath.foreach { entry => test(entry.toNIO.getFileName.toString) {