Skip to content

Commit 9be3303

Browse files
committed
Introduce dedicated backwards compatibility type LegacySessionLoadingPreferenceConfig
1 parent 6919d19 commit 9be3303

2 files changed

Lines changed: 42 additions & 17 deletions

File tree

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import qualified Data.Aeson as A
1717
import Data.Aeson.Lens (_String)
1818
import qualified Data.Aeson.Types as A
1919
import Data.Default
20+
import Data.Functor ((<&>))
2021
import qualified Data.Map.Strict as Map
2122
import Data.Maybe (fromMaybe)
2223
import qualified Data.Text as T
@@ -45,13 +46,14 @@ parseConfig idePlugins defValue = A.withObject "settings" $ \o ->
4546
<*> o .:? "maxCompletions" .!= maxCompletions defValue
4647
<*> loadingPref o
4748
<*> o .:? "linkSourceTo" .!= linkSourceTo defValue
48-
<*> o .:? "linkDocTo" .!=
49-
linkDocTo defValue
49+
<*> o .:? "linkDocTo" .!= linkDocTo defValue
5050
<*> A.explicitParseFieldMaybe (parsePlugins idePlugins) o "plugin" .!= plugins defValue
5151
where
5252
loadingPref o =
53-
(o .:? "componentsLoading"
54-
<|> o .:? "sessionLoading") .!= componentsLoading defValue
53+
-- We can support "componentsLoading" and the legacy "sessionLoading" option.
54+
(o .:? "componentsLoading" .!= componentsLoading defValue)
55+
<|> (o .:? "sessionLoading" .!= LegacySessionLoadingPreferenceConfig (componentsLoading defValue)
56+
<&> getLegacySessionLoadingPreferenceConfig)
5557

5658
-- | Parse the 'PluginConfig'.
5759
-- Since we need to fall back to default values if we do not find one in the input,

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

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ module Ide.Types
2222
, IdeNotification(..)
2323
, IdePlugins(IdePlugins, ipMap)
2424
, DynFlagsModifications(..)
25-
, Config(..), PluginConfig(..), CheckParents(..), SessionLoadingPreferenceConfig(..)
25+
, Config(..), PluginConfig(..), CheckParents(..)
26+
, SessionLoadingPreferenceConfig(..)
27+
, LegacySessionLoadingPreferenceConfig(..)
28+
, getLegacySessionLoadingPreferenceConfig
2629
, OptLinkTo(..)
2730
, ConfigDescriptor(..), defaultConfigDescriptor, configForPlugin
2831
, CustomConfig(..), mkCustomConfig
@@ -78,7 +81,6 @@ import Data.Hashable (Hashable)
7881
import Data.HashMap.Strict (HashMap)
7982
import qualified Data.HashMap.Strict as HashMap
8083
import Data.Kind (Type)
81-
import qualified Data.List as List
8284
import Data.List.Extra (find, sortOn)
8385
import Data.List.NonEmpty (NonEmpty (..), toList)
8486
import qualified Data.Map as Map
@@ -251,13 +253,29 @@ instance Pretty SessionLoadingPreferenceConfig where
251253
pretty PreferMultiWholeProjectLoading = "Prefer Whole Project Loading"
252254

253255
-- | Labels for @SessionLoadingPreferenceConfig@ json format.
256+
--
257+
-- 'singleComponent' and 'multipleComponents' are outdated but are maintained here
258+
-- for backwards compatibility.
259+
-- We prefer 'single', 'multiNeededOnly' and 'multiWholeProject'
254260
singleComponent, multipleComponents, single, multiNeededOnly, multiWholeProject :: T.Text
255261
singleComponent = "singleComponent"
256262
multipleComponents = "multipleComponents"
257263
single = "single"
258264
multiNeededOnly = "multi: needed-only"
259265
multiWholeProject = "multi: whole-project"
260266

267+
-- | Historical artefact!
268+
-- Before HLS 2.15.0.0, the 'SessionLoadingPreferenceConfig' option was called `sessionLoading` with the values
269+
-- `multipleComponents` and `singleComponent`.
270+
--
271+
-- With HLS 2.15.0.0, we renamed these options and also added some new ones!
272+
-- For backwards compatibility, we support the old naming as well using
273+
-- this backwards compatibility newtype.
274+
newtype LegacySessionLoadingPreferenceConfig = LegacySessionLoadingPreferenceConfig SessionLoadingPreferenceConfig
275+
276+
getLegacySessionLoadingPreferenceConfig :: LegacySessionLoadingPreferenceConfig -> SessionLoadingPreferenceConfig
277+
getLegacySessionLoadingPreferenceConfig (LegacySessionLoadingPreferenceConfig conf) = conf
278+
261279
instance ToJSON SessionLoadingPreferenceConfig where
262280
toJSON PreferSingleComponentLoading =
263281
String single
@@ -268,19 +286,24 @@ instance ToJSON SessionLoadingPreferenceConfig where
268286

269287
instance FromJSON SessionLoadingPreferenceConfig where
270288
parseJSON (String val)
271-
| val `elem` [singleComponent, single] = pure PreferSingleComponentLoading
272-
| val `elem` [multipleComponents, multiNeededOnly] = pure PreferMultiComponentLoading
289+
| single == val = pure PreferSingleComponentLoading
290+
| multiNeededOnly == val = pure PreferMultiComponentLoading
273291
| val == multiWholeProject = pure PreferMultiWholeProjectLoading
274292
| otherwise = A.prependFailure "parsing SessionLoadingPreferenceConfig failed, "
275-
(A.parseFail $ unwords ["Expected one of", expected, "but got", T.unpack val] )
276-
where
277-
expected = concat ["[", List.intercalate ", " fields, "]"]
278-
fields = map show
279-
[single, multiNeededOnly, multiWholeProject]
280-
++
281-
[ show old_value ++ " (deprecated)"
282-
| old_value <- [singleComponent,multipleComponents]
283-
]
293+
(A.parseFail $ unwords ["Expected one of " ++ expected ++ " but got", T.unpack val] )
294+
where
295+
expected = T.unpack $ T.intercalate ", " $ map (\ t -> "\'" <> t <> "\'") [single, multiNeededOnly, multiWholeProject]
296+
parseJSON o = A.prependFailure "parsing SessionLoadingPreferenceConfig failed, "
297+
(A.typeMismatch "String" o)
298+
299+
instance FromJSON LegacySessionLoadingPreferenceConfig where
300+
parseJSON (String val)
301+
| singleComponent == val = pure $ LegacySessionLoadingPreferenceConfig PreferSingleComponentLoading
302+
| multipleComponents == val = pure $ LegacySessionLoadingPreferenceConfig PreferMultiComponentLoading
303+
| otherwise = A.prependFailure "parsing SessionLoadingPreferenceConfig failed, "
304+
(A.parseFail $ "Expected one of " ++ expected ++ " but got " <> T.unpack val )
305+
where
306+
expected = T.unpack $ T.intercalate ", " $ map (\ t -> "\'" <> t <> "\'") [singleComponent, multipleComponents]
284307
parseJSON o = A.prependFailure "parsing SessionLoadingPreferenceConfig failed, "
285308
(A.typeMismatch "String" o)
286309

0 commit comments

Comments
 (0)