Skip to content

Commit 26dcc40

Browse files
incorporate changes requested in #4375
1 parent c2d980c commit 26dcc40

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal.hs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import qualified Data.Text as T
2424
import qualified Data.Text.Encoding as Encoding
2525
import Data.Typeable
2626
import Development.IDE as D
27+
import Development.IDE.Core.PluginUtils
2728
import Development.IDE.Core.Shake (restartShakeSession)
2829
import qualified Development.IDE.Core.Shake as Shake
2930
import Development.IDE.Graph (Key, alwaysRerun)
@@ -45,6 +46,7 @@ import qualified Ide.Plugin.Cabal.LicenseSuggest as LicenseSuggest
4546
import Ide.Plugin.Cabal.Orphans ()
4647
import Ide.Plugin.Cabal.Outline
4748
import qualified Ide.Plugin.Cabal.Parse as Parse
49+
import Ide.Plugin.Error
4850
import Ide.Types
4951
import qualified Language.LSP.Protocol.Lens as JL
5052
import qualified Language.LSP.Protocol.Message as LSP
@@ -289,28 +291,23 @@ fieldSuggestCodeAction recorder ide _ (CodeActionParams _ _ (TextDocumentIdentif
289291
-- TODO: Support more definitions than sections.
290292
gotoDefinition :: PluginMethodHandler IdeState LSP.Method_TextDocumentDefinition
291293
gotoDefinition ideState _ msgParam = do
292-
case uriToFilePath' uri of
294+
nfp <- getNormalizedFilePathE uri
295+
cabalFields <- runActionE "cabal-plugin.commonSections" ideState $ useE ParseCabalFields nfp
296+
case CabalFields.findTextWord cursor cabalFields of
293297
Nothing ->
294298
pure $ InR $ InR Null
295-
Just filePath -> do
296-
mCabalFields <- liftIO $ runAction "cabal-plugin.commonSections" ideState $ use ParseCabalFields $ toNormalizedFilePath filePath
297-
let mCursorText = CabalFields.findTextWord cursor =<< mCabalFields
298-
case mCursorText of
299+
Just cursorText -> do
300+
commonSections <- runActionE "cabal-plugin.commonSections" ideState $ useE ParseCabalCommonSections nfp
301+
case find (isSectionArgName cursorText) commonSections of
299302
Nothing ->
300303
pure $ InR $ InR Null
301-
Just cursorText -> do
302-
mCommonSections <- liftIO $ runAction "cabal-plugin.commonSections" ideState $ use ParseCabalCommonSections $ toNormalizedFilePath filePath
303-
let mCommonSection = find (filterSectionArgName cursorText) =<< mCommonSections
304-
case mCommonSection of
305-
Nothing ->
306-
pure $ InR $ InR Null
307-
Just commonSection -> do
308-
pure $ InL $ Definition $ InL $ Location uri $ CabalFields.getFieldLSPRange commonSection
304+
Just commonSection -> do
305+
pure $ InL $ Definition $ InL $ Location uri $ CabalFields.getFieldLSPRange commonSection
309306
where
310307
cursor = Types.lspPositionToCabalPosition (msgParam ^. JL.position)
311308
uri = msgParam ^. JL.textDocument . JL.uri
312-
filterSectionArgName name (Syntax.Section _ sectionArgName _) = name == CabalFields.onelineSectionArgs sectionArgName
313-
filterSectionArgName _ _ = False
309+
isSectionArgName name (Syntax.Section _ sectionArgName _) = name == CabalFields.onelineSectionArgs sectionArgName
310+
isSectionArgName _ _ = False
314311

315312
-- ----------------------------------------------------------------
316313
-- Cabal file of Interest rules and global variable

plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/CabalFields.hs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
2-
module Ide.Plugin.Cabal.Completion.CabalFields (findStanzaForColumn, findFieldSection, findTextWord, findFieldLine, getOptionalSectionName, getAnnotation, getFieldName, onelineSectionArgs, getFieldEndPosition, getSectionArgEndPosition, getNameEndPosition, getFieldLineEndPosition, getFieldLSPRange) where
1+
module Ide.Plugin.Cabal.Completion.CabalFields
2+
( findStanzaForColumn,
3+
findFieldSection,
4+
findTextWord,
5+
findFieldLine,
6+
getOptionalSectionName,
7+
getAnnotation,
8+
getFieldName,
9+
onelineSectionArgs,
10+
getFieldEndPosition,
11+
getSectionArgEndPosition,
12+
getNameEndPosition,
13+
getFieldLineEndPosition,
14+
getFieldLSPRange
15+
) where
316

417
import qualified Data.ByteString as BS
518
import Data.List (find)
@@ -10,7 +23,7 @@ import qualified Data.Text.Encoding as T
1023
import qualified Distribution.Fields as Syntax
1124
import qualified Distribution.Parsec.Position as Syntax
1225
import Ide.Plugin.Cabal.Completion.Types
13-
import qualified Language.LSP.Protocol.Types as LSPTypes
26+
import qualified Language.LSP.Protocol.Types as LSP
1427

1528
-- ----------------------------------------------------------------
1629
-- Cabal-syntax utilities I don't really want to write myself
@@ -32,7 +45,7 @@ findStanzaForColumn col ctx = case NE.uncons ctx of
3245
--
3346
-- The result is said field and its starting position
3447
-- or Nothing if the passed list of fields is empty.
35-
48+
--
3649
-- This only looks at the row of the cursor and not at the cursor's
3750
-- position within the row.
3851
--
@@ -54,7 +67,7 @@ findFieldSection cursor (x:y:ys)
5467
--
5568
-- The result is said field line and its starting position
5669
-- or Nothing if the passed list of fields is empty.
57-
70+
--
5871
-- This function assumes that elements in a field's @FieldLine@ list
5972
-- do not share the same row.
6073
findFieldLine :: Syntax.Position -> [Syntax.Field Syntax.Position] -> Maybe (Syntax.FieldLine Syntax.Position)
@@ -76,7 +89,7 @@ findFieldLine cursor fields =
7689
-- or the cursor position is not next to, or on a word.
7790
-- For this function, a word is a sequence of consecutive characters
7891
-- that are not a space or column.
79-
92+
--
8093
-- This function currently only considers words inside of a @FieldLine@.
8194
findTextWord :: Syntax.Position -> [Syntax.Field Syntax.Position] -> Maybe T.Text
8295
findTextWord _cursor [] = Nothing
@@ -175,9 +188,9 @@ getNameEndPosition (Syntax.Name (Syntax.Position row col) byteString) = Syntax.P
175188
getFieldLineEndPosition :: Syntax.FieldLine Syntax.Position -> Syntax.Position
176189
getFieldLineEndPosition (Syntax.FieldLine (Syntax.Position row col) byteString) = Syntax.Position row (col + BS.length byteString)
177190

178-
-- | Returns a LSP compatible range for a provided field
179-
getFieldLSPRange :: Syntax.Field Syntax.Position -> LSPTypes.Range
180-
getFieldLSPRange field = LSPTypes.Range startLSPPos endLSPPos
191+
-- | Returns an LSP compatible range for a provided field
192+
getFieldLSPRange :: Syntax.Field Syntax.Position -> LSP.Range
193+
getFieldLSPRange field = LSP.Range startLSPPos endLSPPos
181194
where
182195
startLSPPos = cabalPositionToLSPPosition $ getAnnotation field
183196
endLSPPos = cabalPositionToLSPPosition $ getFieldEndPosition field

0 commit comments

Comments
 (0)