Skip to content

Commit a7e08c6

Browse files
committed
Possible TextLines-based implementation
1 parent 4a36269 commit a7e08c6

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

cabal.project

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ tests: True
1010
benchmarks: True
1111
test-show-details: direct
1212
haddock-quickjump: True
13+
14+
source-repository-package
15+
type: git
16+
location: https://github.com/Bodigrim/text-rope
17+
tag: 0394a40476db8083a0b5fcdba2ce97b409727912

lsp/src/Language/LSP/VFS.hs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ import Data.Ord
7474
import qualified Data.HashMap.Strict as HashMap
7575
import qualified Data.Map.Strict as Map
7676
import Data.Maybe
77-
import qualified Data.Text.Rope as URope
77+
import qualified Data.Text.Lines as ULines
7878
import Data.Text.Utf16.Rope ( Rope )
7979
import qualified Data.Text.Utf16.Rope as Rope
80+
import qualified Data.Text.Utf16.Lines as Lines
8081
import Data.Text.Prettyprint.Doc
8182
import qualified Language.LSP.Types as J
8283
import qualified Language.LSP.Types.Lens as J
@@ -367,15 +368,12 @@ data CodePointPosition =
367368
-- We need the file itself because this requires translating between code points and code units.
368369
codePointPositionToPosition :: VirtualFile -> CodePointPosition -> J.Position
369370
codePointPositionToPosition vFile (CodePointPosition cpl cpc) =
370-
let utf16Text = _file_text vFile
371-
-- Transcode to a code-point based rope
372-
utfText = URope.fromText $ Rope.toText utf16Text
373-
-- Split at the given position
374-
(utfPrefix, _) = URope.splitAtPosition (URope.Position (fromIntegral cpl) (fromIntegral cpc)) utfText
375-
-- Transcode the prefix to a code-unit based rope
376-
utf16Prefix = Rope.fromText $ URope.toText utfPrefix
377-
-- Get the length of the transcoded prefix
378-
(Rope.Position cul cuc) = Rope.lengthAsPosition utf16Prefix
371+
let text = _file_text vFile
372+
lines = Rope.toTextLines text
373+
-- Split at the given position in *code points*
374+
(prefix, _) = ULines.splitAtPosition (ULines.Position (fromIntegral cpl) (fromIntegral cpc)) lines
375+
-- Get the length of the prefix in *code units*
376+
(Lines.Position cul cuc) = Lines.lengthAsPosition prefix
379377
in J.Position (fromIntegral cul) (fromIntegral cuc)
380378

381379
-- | Given a virtual file, translate a 'J.Position' in that file into a 'CodePointPosition' in that file.
@@ -384,13 +382,12 @@ codePointPositionToPosition vFile (CodePointPosition cpl cpc) =
384382
-- We need the file itself because this requires translating between code unit and code points.
385383
positionToCodePointPosition :: VirtualFile -> J.Position -> Maybe CodePointPosition
386384
positionToCodePointPosition vFile (J.Position cul cuc) = do
387-
let utf16Text = _file_text vFile
388-
-- Split at the given location
389-
(utf16Prefix, _) <- Rope.splitAtPosition (Rope.Position (fromIntegral cul) (fromIntegral cuc)) utf16Text
390-
-- Transcode the preix to a code-point based rope
391-
let utfPrefix = URope.fromText $ Rope.toText utf16Prefix
392-
-- Get the length of the transcoded prefix
393-
(URope.Position cpl cpc) = URope.lengthAsPosition utfPrefix
385+
let text = _file_text vFile
386+
lines = Rope.toTextLines text
387+
-- Split at the given location in *code units*
388+
(prefix, _) <- Lines.splitAtPosition (Lines.Position (fromIntegral cul) (fromIntegral cuc)) lines
389+
-- Get the length of the prefix in *code points*
390+
let (ULines.Position cpl cpc) = ULines.lengthAsPosition prefix
394391
pure $ CodePointPosition (fromIntegral cpl) (fromIntegral cpc)
395392

396393
-- ---------------------------------------------------------------------

0 commit comments

Comments
 (0)