Skip to content

Commit 25dea6a

Browse files
committed
add realSrcSpanToCodePointRange, realSrcLocToCodePointPosition to Development.IDE.GHC.Error
1 parent 71e6532 commit 25dea6a

File tree

5 files changed

+31
-14
lines changed

5 files changed

+31
-14
lines changed

Diff for: ghcide/src/Development/IDE/GHC/Error.hs

+26
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ module Development.IDE.GHC.Error
1717
, realSrcSpanToRange
1818
, realSrcLocToPosition
1919
, realSrcSpanToLocation
20+
, realSrcSpanToCodePointRange
21+
, realSrcLocToCodePointPosition
2022
, srcSpanToFilename
2123
, rangeToSrcSpan
2224
, rangeToRealSrcSpan
@@ -45,6 +47,8 @@ import Development.IDE.Types.Diagnostics as D
4547
import Development.IDE.Types.Location
4648
import GHC
4749
import Language.LSP.Protocol.Types (isSubrangeOf)
50+
import Language.LSP.VFS (CodePointPosition (CodePointPosition),
51+
CodePointRange (CodePointRange))
4852

4953

5054
diagFromText :: T.Text -> D.DiagnosticSeverity -> SrcSpan -> T.Text -> FileDiagnostic
@@ -86,6 +90,28 @@ realSrcLocToPosition :: RealSrcLoc -> Position
8690
realSrcLocToPosition real =
8791
Position (fromIntegral $ srcLocLine real - 1) (fromIntegral $ srcLocCol real - 1)
8892

93+
-- Note [Unicode support]
94+
-- the current situation is:
95+
-- LSP Positions use UTF-16 code units(Unicode may count as variable columns);
96+
-- GHC use Unicode code points(Unicode count as one column).
97+
-- To support unicode, ideally range should be in lsp standard,
98+
-- and codePoint should be in ghc standard.
99+
-- see https://github.com/haskell/lsp/pull/407
100+
101+
-- | Convert a GHC SrcSpan to CodePointRange
102+
-- see Note [Unicode support]
103+
realSrcSpanToCodePointRange :: RealSrcSpan -> CodePointRange
104+
realSrcSpanToCodePointRange real =
105+
CodePointRange
106+
(realSrcLocToCodePointPosition $ Compat.realSrcSpanStart real)
107+
(realSrcLocToCodePointPosition $ Compat.realSrcSpanEnd real)
108+
109+
-- | Convert a GHC RealSrcLoc to CodePointPosition
110+
-- see Note [Unicode support]
111+
realSrcLocToCodePointPosition :: RealSrcLoc -> CodePointPosition
112+
realSrcLocToCodePointPosition real =
113+
CodePointPosition (fromIntegral $ srcLocLine real - 1) (fromIntegral $ srcLocCol real - 1)
114+
89115
-- | Extract a file name from a GHC SrcSpan (use message for unhelpful ones)
90116
-- FIXME This may not be an _absolute_ file name, needs fixing.
91117
srcSpanToFilename :: SrcSpan -> Maybe FilePath

Diff for: plugins/hls-semantic-tokens-plugin/src/Ide/Plugin/SemanticTokens/Internal.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ semanticTokensFull state _ param = do
9494
-- Visible names from 'tmrRenamed'
9595
--
9696
-- It then combines this information to compute the semantic tokens for the file.
97-
getSemanticTokensRule :: Recorder (WithPriority SemanticLog) -> Rules ()
97+
getSemanticTokensRule :: Recorder (WithPriority SemanticLog) -> Rules ()
9898
getSemanticTokensRule recorder =
9999
define (cmapWithPrio LogShake recorder) $ \GetSemanticTokens nfp -> handleError recorder $ do
100100
(HAR {..}) <- lift $ use_ GetHieAst nfp

Diff for: plugins/hls-semantic-tokens-plugin/src/Ide/Plugin/SemanticTokens/Mappings.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import qualified Data.Set as Set
2020
import Data.Text (Text, unpack)
2121
import Development.IDE (HieKind (HieFresh, HieFromDisk))
2222
import Development.IDE.GHC.Compat
23-
import Ide.Plugin.SemanticTokens.Types hiding (tokens)
23+
import Ide.Plugin.SemanticTokens.Types
2424
import Ide.Plugin.SemanticTokens.Utils (mkRange, recoverFunMaskArray)
2525
import Language.LSP.Protocol.Types (LspEnum (knownValues),
2626
SemanticTokenAbsolute (SemanticTokenAbsolute),

Diff for: plugins/hls-semantic-tokens-plugin/src/Ide/Plugin/SemanticTokens/Query.hs

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import Data.Text (Text)
1919
import Development.IDE.Core.PositionMapping (PositionMapping,
2020
toCurrentRange)
2121
import Development.IDE.GHC.Compat
22+
import Development.IDE.GHC.Error (realSrcSpanToCodePointRange)
2223
import Ide.Plugin.SemanticTokens.Mappings
2324
import Ide.Plugin.SemanticTokens.Types (HieFunMaskKind,
2425
HsSemanticTokenType,
2526
NameSemanticMap)
26-
import Ide.Plugin.SemanticTokens.Utils (realSrcSpanToCodePointRange)
2727
import Language.LSP.Protocol.Types
2828
import Language.LSP.VFS (VirtualFile,
2929
codePointRangeToRange)
@@ -50,6 +50,8 @@ nameNameSemanticFromHie hieKind rm ns = do
5050
nameSemanticFromRefMap rm' name' = do
5151
spanInfos <- -- traceShow ("getting spans:", nameString) $
5252
Map.lookup (Right name') rm'
53+
-- let combinedFunction x = (identType . snd) x <|> (identInfo . snd) x
54+
-- let result = foldMap (typeSemantic hieKind) $ listToMaybe $ mapMaybe combinedFunction spanInfos
5355
let typeTokenType = foldMap (typeSemantic hieKind) $ listToMaybe $ mapMaybe (identType . snd) spanInfos
5456
contextInfoTokenType <- foldMap (contextInfosMaybeTokenType . identInfo . snd) spanInfos
5557
fold [typeTokenType, Just contextInfoTokenType]

Diff for: plugins/hls-semantic-tokens-plugin/src/Ide/Plugin/SemanticTokens/Utils.hs

-11
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,7 @@ recoverFunMaskArray flattened = unflattened
123123
go (HTyConApp _ _) = False
124124

125125

126-
realSrcSpanToCodePointRange :: RealSrcSpan -> CodePointRange
127-
realSrcSpanToCodePointRange real =
128-
CodePointRange (realSrcLocToCodePointPosition $ Compat.realSrcSpanStart real)
129-
(realSrcLocToCodePointPosition $ Compat.realSrcSpanEnd real)
130-
131-
132-
realSrcLocToCodePointPosition :: RealSrcLoc -> CodePointPosition
133-
realSrcLocToCodePointPosition real =
134-
CodePointPosition (fromIntegral $ srcLocLine real - 1) (fromIntegral $ srcLocCol real - 1)
135-
136126
-- rangeToCodePointRange
137-
-- mkRange :: Int -> Int -> Int -> Range
138127
mkRange :: (Integral a1, Integral a2) => a1 -> a2 -> a2 -> Range
139128
mkRange startLine startCol len =
140129
Range (Position (fromIntegral startLine) (fromIntegral startCol)) (Position (fromIntegral startLine) (fromIntegral $ startCol + len))

0 commit comments

Comments
 (0)