Skip to content

Commit

Permalink
Fix #26 - don't add redundant prelude import
Browse files Browse the repository at this point in the history
  • Loading branch information
nwolverson committed Mar 2, 2018
1 parent 190e8c1 commit 749502f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/LanguageServer/IdePurescript/Imports.purs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,19 @@ 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)
version <- liftEff $ getVersion doc
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
Expand All @@ -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 =
Expand Down
1 change: 0 additions & 1 deletion src/LanguageServer/IdePurescript/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/LanguageServer/Text.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 749502f

Please sign in to comment.