Skip to content

Commit 97e1ae3

Browse files
authored
Merge pull request #419 from vekatze/better-highlighting
improve symbol highlighting
2 parents 1ca7b45 + 34a08b0 commit 97e1ae3

6 files changed

Lines changed: 46 additions & 64 deletions

File tree

src/Command/LSP/Internal/FindDefinition.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ findDefinition ::
3232
(J.HasTextDocument p a1, J.HasUri a1 Uri, J.HasPosition p Position) =>
3333
Handle ->
3434
p ->
35-
App ((LT.LocType, DefinitionLink), LocationTree)
35+
App ((LT.SymbolName, DefinitionLink), LocationTree)
3636
findDefinition h params = do
3737
src <- GetSource.getSource (getSourceHandle h) params
3838
locTree <- GetLocationTree.getLocationTree (getLocationTreeHandle h) src
@@ -43,19 +43,19 @@ _findDefinition ::
4343
(J.HasPosition p Position) =>
4444
p ->
4545
LT.LocationTree ->
46-
Maybe (LT.LocType, DefinitionLink)
46+
Maybe (LT.SymbolName, DefinitionLink)
4747
_findDefinition params locationTree = do
4848
let line = fromEnum (params ^. J.position . J.line) + 1
4949
let col = fromEnum (params ^. J.position . J.character) + 1
50-
(locType, m, _, symbolLen) <- LT.find line col locationTree
50+
(symbolName, m, _, symbolLen) <- LT.find line col locationTree
5151
let defPath = H.metaFileName m
5252
let (defLine, defCol) = H.metaLocation m
5353
let defFilePath' = filePathToUri defPath
5454
let _start = Position {_line = fromIntegral (defLine - 1), _character = fromIntegral (defCol - 1)}
5555
let _end = _start {_character = _character _start + fromIntegral symbolLen}
5656
let range = Range {_start, _end}
5757
return
58-
( locType,
58+
( symbolName,
5959
DefinitionLink $
6060
LocationLink
6161
{ _originSelectionRange = Nothing,

src/Command/LSP/Internal/GetSymbolInfo.hs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ getSymbolInfo params = do
4040
liftMaybe Nothing
4141
Just handle -> do
4242
let findDefHandle = FindDefinition.new h
43-
((locType, _), _) <- FindDefinition.findDefinition findDefHandle params
44-
symbolName <- liftMaybe $ getSymbolLoc locType
43+
((symbolName, _), _) <- FindDefinition.findDefinition findDefHandle params
4544
case symbolName of
4645
LT.Local varID _ -> do
4746
weakTypeEnv <- liftIO $ Elaborate.getWeakTypeEnv handle
@@ -54,11 +53,7 @@ getSymbolInfo params = do
5453
return $ toTextType t
5554
LT.Foreign {} -> do
5655
liftMaybe Nothing
57-
58-
getSymbolLoc :: LT.LocType -> Maybe LT.SymbolName
59-
getSymbolLoc locType =
60-
case locType of
61-
LT.FileLoc ->
62-
Nothing
63-
LT.SymbolLoc symbolName ->
64-
return symbolName
56+
LT.StaticFile {} -> do
57+
liftMaybe Nothing
58+
LT.SourceFile {} -> do
59+
liftMaybe Nothing

src/Kernel/Common/Handle/Local/Tag.hs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ module Kernel.Common.Handle.Local.Tag
22
( Handle (..),
33
new,
44
get,
5-
insertFileLoc,
65
insertLocalVar,
76
insertGlobalVar,
87
insertBinder,
98
insertLocator,
109
insertExternalName,
10+
insertStaticFile,
11+
insertSourceFile,
1112
)
1213
where
1314

@@ -36,18 +37,11 @@ get :: Handle -> IO LT.LocationTree
3637
get h =
3738
readIORef $ _tagMapRef h
3839

39-
insertFileLoc :: Handle -> Hint -> Int -> Hint -> IO ()
40-
insertFileLoc h mUse nameLength mDef = do
41-
when (metaShouldSaveLocation mUse) $ do
42-
let (l, c) = metaLocation mUse
43-
modifyIORef' (_tagMapRef h) $ LT.insert LT.FileLoc (l, (c, c + nameLength)) mDef
44-
4540
insertLocalVar :: Handle -> Hint -> Ident -> Hint -> IO ()
4641
insertLocalVar h mUse ident@(I (var, varID)) mDef = do
4742
unless (isHole ident) $ do
4843
let nameLength = T.length var
49-
let symbolLoc = LT.SymbolLoc (LT.Local varID nameLength)
50-
insert h mUse symbolLoc nameLength mDef
44+
insert h mUse (LT.Local varID nameLength) nameLength mDef
5145

5246
insertBinder :: Handle -> BinderF a -> IO ()
5347
insertBinder h (m, _, ident, _) =
@@ -56,21 +50,29 @@ insertBinder h (m, _, ident, _) =
5650
insertGlobalVar :: Handle -> Hint -> DD.DefiniteDescription -> IsConstLike -> Hint -> IO ()
5751
insertGlobalVar h mUse dd isConstLike mDef = do
5852
let nameLength = T.length (DD.localLocator dd)
59-
let symbolLoc = LT.SymbolLoc (LT.Global dd isConstLike)
60-
insert h mUse symbolLoc nameLength mDef
53+
insert h mUse (LT.Global dd isConstLike) nameLength mDef
6154

62-
insert :: Handle -> Hint -> LT.LocType -> Int -> Hint -> IO ()
63-
insert h mUse locType nameLength mDef = do
55+
insert :: Handle -> Hint -> LT.SymbolName -> Int -> Hint -> IO ()
56+
insert h mUse symbolName nameLength mDef = do
6457
when (metaShouldSaveLocation mUse) $ do
6558
let (l, c) = metaLocation mUse
66-
modifyIORef' (_tagMapRef h) $ LT.insert locType (l, (c, c + nameLength)) mDef
59+
modifyIORef' (_tagMapRef h) $ LT.insert symbolName (l, (c, c + nameLength)) mDef
6760

6861
insertLocator :: Handle -> Hint -> DD.DefiniteDescription -> IsConstLike -> Int -> Hint -> IO ()
6962
insertLocator h mUse dd isConstLike nameLength mDef = do
70-
insert h mUse (LT.SymbolLoc (LT.Global dd isConstLike)) nameLength mDef
63+
insert h mUse (LT.Global dd isConstLike) nameLength mDef
7164

7265
insertExternalName :: Handle -> Hint -> EN.ExternalName -> Hint -> IO ()
7366
insertExternalName h mUse externalName mDef = do
74-
let symbolLoc = LT.SymbolLoc (LT.Foreign externalName)
7567
let nameLength = T.length $ EN.reify externalName
76-
insert h mUse symbolLoc nameLength mDef
68+
insert h mUse (LT.Foreign externalName) nameLength mDef
69+
70+
insertStaticFile :: Handle -> Hint -> T.Text -> Hint -> IO ()
71+
insertStaticFile h mUse key mDef = do
72+
let nameLength = T.length key
73+
insert h mUse (LT.StaticFile key) nameLength mDef
74+
75+
insertSourceFile :: Handle -> Hint -> T.Text -> Hint -> IO ()
76+
insertSourceFile h mUse locator mDef = do
77+
let nameLength = T.length locator
78+
insert h mUse (LT.SourceFile locator) nameLength mDef

src/Kernel/Common/LocationTree.hs

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module Kernel.Common.LocationTree
22
( LocationTree,
3-
LocType (..),
43
SymbolName (..),
54
empty,
65
insert,
@@ -35,39 +34,29 @@ data SymbolName
3534
= Local Int DefSymbolLen
3635
| Global DD.DefiniteDescription IsConstLike
3736
| Foreign EN.ExternalName
38-
deriving (Show, Eq, Generic)
39-
40-
data LocType
41-
= FileLoc
42-
| SymbolLoc SymbolName
37+
| StaticFile T.Text
38+
| SourceFile T.Text
4339
deriving (Show, Eq, Generic)
4440

4541
instance Binary SymbolName
4642

47-
instance Binary LocType
48-
4943
type LocationTree =
50-
M.Map (Line, ColFrom) (Line, ColInterval, LocType, SavedHint)
44+
M.Map (Line, ColFrom) (Line, ColInterval, SymbolName, SavedHint)
5145

5246
empty :: LocationTree
5347
empty =
5448
M.empty
5549

56-
insert :: LocType -> (Line, ColInterval) -> Hint -> LocationTree -> LocationTree
57-
insert lt (l, (cFrom, cTo)) m =
58-
M.insert (l, cFrom) (l, (cFrom, cTo), lt, SavedHint m)
50+
insert :: SymbolName -> (Line, ColInterval) -> Hint -> LocationTree -> LocationTree
51+
insert sym (l, (cFrom, cTo)) m =
52+
M.insert (l, cFrom) (l, (cFrom, cTo), sym, SavedHint m)
5953

60-
find :: Line -> Column -> LocationTree -> Maybe (LocType, Hint, ColInterval, DefSymbolLen)
54+
find :: Line -> Column -> LocationTree -> Maybe (SymbolName, Hint, ColInterval, DefSymbolLen)
6155
find l c mp = do
62-
(line, colInterval@(colFrom, colTo), lt, SavedHint m) <- snd <$> M.lookupLE (l, c) mp
56+
(line, colInterval@(_, colTo), sym, SavedHint m) <- snd <$> M.lookupLE (l, c) mp
6357
if colTo < c || line /= l
6458
then Nothing
65-
else do
66-
case lt of
67-
FileLoc ->
68-
return (lt, m, colInterval, colTo - colFrom)
69-
SymbolLoc sym ->
70-
return (lt, m, colInterval, getLength sym)
59+
else return (sym, m, colInterval, getLength sym)
7160

7261
getLength :: SymbolName -> DefSymbolLen
7362
getLength s =
@@ -78,19 +67,15 @@ getLength s =
7867
T.length $ DD.localLocator dd
7968
Foreign externalName ->
8069
T.length $ EN.reify externalName
81-
82-
isSymLoc :: LocType -> Bool
83-
isSymLoc lt =
84-
case lt of
85-
SymbolLoc _ ->
86-
True
87-
FileLoc ->
88-
False
70+
StaticFile key ->
71+
T.length key
72+
SourceFile locator ->
73+
T.length locator
8974

9075
findRef :: Loc -> LocationTree -> [(FilePath, (Line, ColInterval))]
9176
findRef loc t = do
9277
let kvs = M.toList t
93-
flip mapMaybe kvs $ \((line, _), (_, colInterval, lt, SavedHint m)) -> do
94-
if isSymLoc lt && loc == metaLocation m
78+
flip mapMaybe kvs $ \((line, _), (_, colInterval, _, SavedHint m)) -> do
79+
if loc == metaLocation m
9580
then return (metaFileName m, (line, colInterval))
9681
else Nothing

src/Kernel/Parse/Internal/Discern.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ discern h term =
649649
case contentOrNone of
650650
Just (path, content) -> do
651651
liftIO $ Unused.deleteStaticFile (H.unusedHandle h) key
652-
liftIO $ Tag.insertFileLoc (H.tagHandle h) mKey (T.length key) (newSourceHint path)
652+
liftIO $ Tag.insertStaticFile (H.tagHandle h) mKey key (newSourceHint path)
653653
return $ m :< WT.Prim (WPV.Text content)
654654
Nothing ->
655655
raiseError m $ "No such static file is defined: `" <> key <> "`"

src/Kernel/Parse/Internal/Import.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ interpretImportItemTextFile h currentModule keyList = do
101101
case Map.lookup key (moduleTextFiles currentModule') of
102102
Just path -> do
103103
let fullPath = moduleRootDir </> path
104-
liftIO $ Tag.insertFileLoc (tagHandle h) mKey (T.length key) (newSourceHint fullPath)
104+
liftIO $ Tag.insertStaticFile (tagHandle h) mKey key (newSourceHint fullPath)
105105
liftIO $ Unused.insertStaticFile (unusedHandle h) key mKey
106106
return (key, (mKey, fullPath))
107107
Nothing ->
@@ -143,7 +143,7 @@ getSource h mustUpdateTag m sgl locatorText = do
143143
let nextPath = getSourceDir nextModule </> relPath
144144
when mustUpdateTag $
145145
liftIO $
146-
Tag.insertFileLoc (tagHandle h) m (T.length locatorText) (newSourceHint nextPath)
146+
Tag.insertSourceFile (tagHandle h) m locatorText (newSourceHint nextPath)
147147
STL.shiftToLatest
148148
(shiftToLatestHandle h)
149149
Source.Source

0 commit comments

Comments
 (0)