Skip to content

Commit d81aad8

Browse files
author
kokobd
committed
add empty selection range handler
1 parent e622744 commit d81aad8

File tree

7 files changed

+110
-47
lines changed

7 files changed

+110
-47
lines changed

cabal.project

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ package *
3838
ghc-options: -haddock
3939
test-show-details: direct
4040

41+
source-repository-package
42+
type: git
43+
location: https://github.com/kokobd/lsp.git
44+
tag: 1ee82f3fb1539350b2726b9b2d979abd9081ec7a
45+
subdir: lsp-types
46+
4147
write-ghc-environment-files: never
4248

4349
index-state: 2021-12-29T12:30:08Z

ghcide/ghcide.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ library
205205
Development.IDE.Plugin.HLS.GhcIde
206206
Development.IDE.Plugin.Test
207207
Development.IDE.Plugin.TypeLenses
208+
Development.IDE.Plugin.SelectionRange
208209

209210
other-modules:
210211
Development.IDE.Core.FileExists

ghcide/src/Development/IDE/Core/Actions.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Development.IDE.Core.Actions
1010
, useNoFileE
1111
, usesE
1212
, workspaceSymbols
13+
, getSelectionRanges
1314
) where
1415

1516
import Control.Monad.Reader
@@ -28,8 +29,10 @@ import Development.IDE.Graph
2829
import qualified Development.IDE.Spans.AtPoint as AtPoint
2930
import Development.IDE.Types.HscEnvEq (hscEnv)
3031
import Development.IDE.Types.Location
32+
import Development.IDE.Types.Logger
3133
import qualified HieDb
3234
import Language.LSP.Types (DocumentHighlight (..),
35+
SelectionRange,
3336
SymbolInformation (..))
3437

3538

@@ -119,3 +122,10 @@ workspaceSymbols query = runMaybeT $ do
119122
ShakeExtras{withHieDb} <- ask
120123
res <- liftIO $ withHieDb (\hieDb -> HieDb.searchDef hieDb $ T.unpack query)
121124
pure $ mapMaybe AtPoint.defRowToSymbolInfo res
125+
126+
getSelectionRanges :: NormalizedFilePath -> [Position] -> IdeAction [SelectionRange]
127+
getSelectionRanges file positions = fmap (fromMaybe []) <$> runMaybeT $ do
128+
ShakeExtras{logger} <- ask
129+
(parsedModule, _) <- useE GetParsedModuleWithComments file
130+
liftIO $ logDebug logger $ T.pack (show parsedModule)
131+
pure []

ghcide/src/Development/IDE/Plugin/HLS/GhcIde.hs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ module Development.IDE.Plugin.HLS.GhcIde
99
import Control.Monad.IO.Class
1010
import Development.IDE
1111
import Development.IDE.LSP.HoverDefinition
12-
import qualified Development.IDE.LSP.Notifications as Notifications
12+
import qualified Development.IDE.LSP.Notifications as Notifications
1313
import Development.IDE.LSP.Outline
14-
import qualified Development.IDE.Plugin.CodeAction as CodeAction
15-
import qualified Development.IDE.Plugin.Completions as Completions
16-
import qualified Development.IDE.Plugin.TypeLenses as TypeLenses
14+
import qualified Development.IDE.Plugin.CodeAction as CodeAction
15+
import qualified Development.IDE.Plugin.Completions as Completions
16+
import qualified Development.IDE.Plugin.SelectionRange as SelectionRange
17+
import qualified Development.IDE.Plugin.TypeLenses as TypeLenses
1718
import Ide.Types
18-
import Language.LSP.Server (LspM)
19+
import Language.LSP.Server (LspM)
1920
import Language.LSP.Types
20-
import Text.Regex.TDFA.Text ()
21+
import Text.Regex.TDFA.Text ()
2122

2223
descriptors :: [PluginDescriptor IdeState]
2324
descriptors =
@@ -28,6 +29,7 @@ descriptors =
2829
CodeAction.fillHolePluginDescriptor "ghcide-code-actions-fill-holes",
2930
Completions.descriptor "ghcide-completions",
3031
TypeLenses.descriptor "ghcide-type-lenses",
32+
SelectionRange.descriptor "ghcide-selection-range",
3133
Notifications.descriptor "ghcide-core"
3234
]
3335

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Development.IDE.Plugin.SelectionRange
2+
( descriptor
3+
) where
4+
5+
import Control.Monad.IO.Class (liftIO)
6+
import Development.IDE (IdeState (shakeExtras),
7+
runIdeAction,
8+
toNormalizedFilePath',
9+
uriToFilePath')
10+
import Development.IDE.Core.Actions (getSelectionRanges)
11+
import Ide.Types (PluginDescriptor (pluginHandlers),
12+
PluginId,
13+
defaultPluginDescriptor,
14+
mkPluginHandler)
15+
import Language.LSP.Server (LspM)
16+
import Language.LSP.Types (List (List), ResponseError,
17+
SMethod (STextDocumentSelectionRange),
18+
SelectionRange,
19+
SelectionRangeParams (..),
20+
TextDocumentIdentifier (TextDocumentIdentifier))
21+
22+
descriptor :: PluginId -> PluginDescriptor IdeState
23+
descriptor plId = (defaultPluginDescriptor plId)
24+
{ pluginHandlers = mkPluginHandler STextDocumentSelectionRange selectionRangeHandler
25+
}
26+
27+
selectionRangeHandler :: IdeState -> PluginId -> SelectionRangeParams -> LspM c (Either ResponseError (List SelectionRange))
28+
selectionRangeHandler ide _ SelectionRangeParams{..} = do
29+
let (TextDocumentIdentifier uri) = _textDocument
30+
let filePathMaybe = toNormalizedFilePath' <$> uriToFilePath' uri
31+
case filePathMaybe of
32+
Nothing -> pure . Right . List $ []
33+
Just filePath -> liftIO $ do
34+
let (List positions) = _positions
35+
selectionRanges <- runIdeAction "SelectionRange" (shakeExtras ide) $ getSelectionRanges filePath positions
36+
pure . Right . List $ selectionRanges

hls-plugin-api/src/Ide/Plugin/Config.hs

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -100,58 +100,62 @@ instance A.ToJSON Config where
100100
-- This provides a regular naming scheme for all plugin config.
101101
data PluginConfig =
102102
PluginConfig
103-
{ plcGlobalOn :: !Bool
104-
, plcCallHierarchyOn :: !Bool
105-
, plcCodeActionsOn :: !Bool
106-
, plcCodeLensOn :: !Bool
107-
, plcDiagnosticsOn :: !Bool
108-
, plcHoverOn :: !Bool
109-
, plcSymbolsOn :: !Bool
110-
, plcCompletionOn :: !Bool
111-
, plcRenameOn :: !Bool
112-
, plcConfig :: !A.Object
103+
{ plcGlobalOn :: !Bool
104+
, plcCallHierarchyOn :: !Bool
105+
, plcCodeActionsOn :: !Bool
106+
, plcCodeLensOn :: !Bool
107+
, plcDiagnosticsOn :: !Bool
108+
, plcHoverOn :: !Bool
109+
, plcSymbolsOn :: !Bool
110+
, plcCompletionOn :: !Bool
111+
, plcRenameOn :: !Bool
112+
, plcSelectionRangeOn :: !Bool
113+
, plcConfig :: !A.Object
113114
} deriving (Show,Eq)
114115

115116
instance Default PluginConfig where
116117
def = PluginConfig
117-
{ plcGlobalOn = True
118-
, plcCallHierarchyOn = True
119-
, plcCodeActionsOn = True
120-
, plcCodeLensOn = True
121-
, plcDiagnosticsOn = True
122-
, plcHoverOn = True
123-
, plcSymbolsOn = True
124-
, plcCompletionOn = True
125-
, plcRenameOn = True
126-
, plcConfig = mempty
118+
{ plcGlobalOn = True
119+
, plcCallHierarchyOn = True
120+
, plcCodeActionsOn = True
121+
, plcCodeLensOn = True
122+
, plcDiagnosticsOn = True
123+
, plcHoverOn = True
124+
, plcSymbolsOn = True
125+
, plcCompletionOn = True
126+
, plcRenameOn = True
127+
, plcSelectionRangeOn = True
128+
, plcConfig = mempty
127129
}
128130

129131
instance A.ToJSON PluginConfig where
130-
toJSON (PluginConfig g ch ca cl d h s c rn cfg) = r
132+
toJSON (PluginConfig g ch ca cl d h s c rn sr cfg) = r
131133
where
132-
r = object [ "globalOn" .= g
133-
, "callHierarchyOn" .= ch
134-
, "codeActionsOn" .= ca
135-
, "codeLensOn" .= cl
136-
, "diagnosticsOn" .= d
137-
, "hoverOn" .= h
138-
, "symbolsOn" .= s
139-
, "completionOn" .= c
140-
, "renameOn" .= rn
141-
, "config" .= cfg
134+
r = object [ "globalOn" .= g
135+
, "callHierarchyOn" .= ch
136+
, "codeActionsOn" .= ca
137+
, "codeLensOn" .= cl
138+
, "diagnosticsOn" .= d
139+
, "hoverOn" .= h
140+
, "symbolsOn" .= s
141+
, "completionOn" .= c
142+
, "renameOn" .= rn
143+
, "selectionRangeOn" .= sr
144+
, "config" .= cfg
142145
]
143146

144147
instance A.FromJSON PluginConfig where
145148
parseJSON = A.withObject "PluginConfig" $ \o -> PluginConfig
146-
<$> o .:? "globalOn" .!= plcGlobalOn def
147-
<*> o .:? "callHierarchyOn" .!= plcCallHierarchyOn def
148-
<*> o .:? "codeActionsOn" .!= plcCodeActionsOn def
149-
<*> o .:? "codeLensOn" .!= plcCodeLensOn def
150-
<*> o .:? "diagnosticsOn" .!= plcDiagnosticsOn def -- AZ
151-
<*> o .:? "hoverOn" .!= plcHoverOn def
152-
<*> o .:? "symbolsOn" .!= plcSymbolsOn def
153-
<*> o .:? "completionOn" .!= plcCompletionOn def
154-
<*> o .:? "renameOn" .!= plcRenameOn def
155-
<*> o .:? "config" .!= plcConfig def
149+
<$> o .:? "globalOn" .!= plcGlobalOn def
150+
<*> o .:? "callHierarchyOn" .!= plcCallHierarchyOn def
151+
<*> o .:? "codeActionsOn" .!= plcCodeActionsOn def
152+
<*> o .:? "codeLensOn" .!= plcCodeLensOn def
153+
<*> o .:? "diagnosticsOn" .!= plcDiagnosticsOn def -- AZ
154+
<*> o .:? "hoverOn" .!= plcHoverOn def
155+
<*> o .:? "symbolsOn" .!= plcSymbolsOn def
156+
<*> o .:? "completionOn" .!= plcCompletionOn def
157+
<*> o .:? "renameOn" .!= plcRenameOn def
158+
<*> o .:? "selectionRangeOn" .!= plcSelectionRangeOn def
159+
<*> o .:? "config" .!= plcConfig def
156160

157161
-- ---------------------------------------------------------------------

hls-plugin-api/src/Ide/Types.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,10 @@ instance PluginMethod TextDocumentRangeFormatting where
284284
instance PluginMethod TextDocumentPrepareCallHierarchy where
285285
pluginEnabled _ = pluginEnabledConfig plcCallHierarchyOn
286286

287+
instance PluginMethod TextDocumentSelectionRange where
288+
pluginEnabled _ = pluginEnabledConfig plcSelectionRangeOn
289+
combineResponses _ _ _ _ (x :| _) = x
290+
287291
instance PluginMethod CallHierarchyIncomingCalls where
288292
pluginEnabled _ = pluginEnabledConfig plcCallHierarchyOn
289293

0 commit comments

Comments
 (0)