diff --git a/src/LanguageServer/IdePurescript/Imports.purs b/src/LanguageServer/IdePurescript/Imports.purs index ccb6ffa..99f881b 100644 --- a/src/LanguageServer/IdePurescript/Imports.purs +++ b/src/LanguageServer/IdePurescript/Imports.purs @@ -26,10 +26,9 @@ import LanguageServer.Uri (uriToFilename) import PscIde.Command as C addCompletionImport :: forall eff. Notify (MainEff eff) -> DocumentStore -> Settings -> ServerState (MainEff eff) -> Array Foreign -> Aff (MainEff eff) Foreign -addCompletionImport log docs config state args = do +addCompletionImport log docs config state@(ServerState { port, modules, conn }) args = do let shouldAddImport = autocompleteAddImport config prelude = preludeModule config - ServerState { port, modules, conn } = state case port, (runExcept <<< readString) <$> args, shouldAddImport of Just port, [ Right identifier, mod, qual, Right uri ], true -> do doc <- liftEff $ getDocument docs (DocumentUri uri) @@ -37,9 +36,9 @@ addCompletionImport log docs config state args = do text <- liftEff $ getText doc { state: modulesState', result } <- case hush mod, hush qual of - Just mod', Just qual' | all (not (isSameQualified mod' qual') <<< unwrap) modules.modules -> + Just mod', Just qual' | noModule (isSameQualified mod' qual') -> addQualifiedImport modules port uri text mod' qual' - Just mod', Nothing | mod' == prelude -> do + Just mod', Nothing | mod' == prelude && noModule (isSameUnqualified prelude) -> addOpenImport modules port uri text mod' mod', qual' -> addExplicitImport modules port uri text mod' qual' identifier @@ -62,10 +61,15 @@ addCompletionImport log docs config state args = do where successResult = toForeign $ toNullable Nothing + noModule f = all (not f <<< unwrap) modules.modules isSameQualified mod qual = case _ of { moduleName: mod', qualifier: Just qual'} -> mod == mod' && qual == qual' _ -> false + isSameUnqualified mod = case _ of + { moduleName, qualifier: Nothing } -> mod == moduleName + _ -> false + -- addModuleImport discards the result data type and wraps it in Maybe. We -- need to add it back for the types to unify. addOpenImport modules port uri text mod = diff --git a/src/LanguageServer/IdePurescript/Main.purs b/src/LanguageServer/IdePurescript/Main.purs index 43fc6b0..d71d392 100644 --- a/src/LanguageServer/IdePurescript/Main.purs +++ b/src/LanguageServer/IdePurescript/Main.purs @@ -43,7 +43,6 @@ import LanguageServer.TextDocument (getText, getUri) import LanguageServer.Types (Diagnostic, DocumentUri(..), FileChangeType(..), FileChangeTypeCode(..), FileEvent(..), Settings, TextDocumentIdentifier(..), intToFileChangeType) import LanguageServer.Uri (filenameToUri, uriToFilename) import Node.Process (argv, cwd) -import PscIde (load) defaultServerState :: forall eff. ServerState eff defaultServerState = ServerState diff --git a/src/LanguageServer/Text.purs b/src/LanguageServer/Text.purs index 5bc1385..7f5880a 100644 --- a/src/LanguageServer/Text.purs +++ b/src/LanguageServer/Text.purs @@ -5,7 +5,7 @@ import Prelude import Data.Array (findIndex, last, length, null, reverse, slice, zip) import Data.Either (either) import Data.Maybe (Maybe(..)) -import Data.String (Pattern(..), joinWith, split) +import Data.String (joinWith) import Data.String.Regex (regex) import Data.String.Regex as Regex import Data.String.Regex.Flags (noFlags)