Skip to content

Commit

Permalink
Added vim module completion.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcWeber authored and nominolo committed May 25, 2009
1 parent 64e6e7e commit fd9079b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
18 changes: 18 additions & 0 deletions lib/Scion/Utils.hs
Expand Up @@ -23,6 +23,8 @@ import Outputable
import Control.Monad
import Data.Maybe ( fromMaybe )

import Data.Char (isLower, isUpper)

thingsAroundPoint :: (Int, Int) -> [Located n] -> [Located n]
thingsAroundPoint pt ls = [ l | l <- ls, spans (getLoc l) pt ]

Expand Down Expand Up @@ -55,3 +57,19 @@ ifM :: Monad m => m Bool -> m a -> m a -> m a
ifM cm tm em = do
c <- cm
if c then tm else em

-- an alternative to the broken Fuzzy module
-- match sH simpleHTTP
-- match siH simpleHTTP
-- match sHTTP simpleHTTP
-- match pSL putStrLn
-- match lM liftM
-- match DS Data.Set
camelCaseMatch :: String -> String -> Bool
camelCaseMatch (c:cs) (i:is)
| c == i = (camelCaseMatch cs $ dropWhile (\c' -> isLower c' || c' == '.') . dropWhile isUpper $ is)
|| camelCaseMatch cs is -- to allow siH match simpleHTTP
| otherwise = False
camelCaseMatch [] [] = True
camelCaseMatch [] _ = False
camelCaseMatch _ [] = False
16 changes: 14 additions & 2 deletions server/Scion/Server/Protocol/Vim.hs
Expand Up @@ -23,7 +23,7 @@ import Scion.Inspect ( prettyResult )
import Scion.Inspect.Find ( overlaps, findHsThing, pathToDeepest)
import Scion.Inspect.TypeOf ( typeOf )
import Scion.Configure (configureCabalProject)
import Scion.Utils ( unqualifiedForModule )
import Scion.Utils ( unqualifiedForModule, camelCaseMatch )
import Scion.Session (preprocessPackage, currentCabalPackage, loadComponent,
backgroundTypecheckFile, unload, setGHCVerbosity, addCmdLineFlags)
import FastString (fsLit, unpackFS)
Expand All @@ -37,7 +37,7 @@ import qualified System.Log.Logger as HL

import qualified Data.ByteString.Char8 as S
import qualified Data.Map as M
import Data.List (intercalate, nub)
import Data.List (intercalate, nub, isPrefixOf)
import Data.Time.Clock ( NominalDiffTime )

import DynFlags ( supportedLanguages, allFlags )
Expand Down Expand Up @@ -89,6 +89,7 @@ vimCommands =
, cmdForceUnload
, cmdAddCmdLineFlag
, cmdThingAtPoint
, cmdModuleCompletion
-- for testing. I'd like to get the module which is exporting the thing one day..
-- basically its the same as cmdThingAtPoint
, cmdThingAtPointMoreInfo
Expand Down Expand Up @@ -248,6 +249,17 @@ cmdThingAtPoint = VimCommand "cmdThingAtPoint" $ \map' -> do
_ -> return (Just (O.showSDocDebug (O.ppr x O.$$ O.ppr xs )))
_ -> return Nothing

-- module completion
cmdModuleCompletion = VimCommand "cmdModuleCompletion" $ \map' -> do
short <- requireArg map' "short"
camelCase <- lookupAndReadFail map' "camelCase"
mod_names <- allExposedModules
let modules = map (O.showSDoc . O.ppr) mod_names
let filterFunc = if camelCase
then \c s -> isPrefixOf c s || camelCaseMatch c s
else isPrefixOf
return $ toVim $ filter (filterFunc short) modules

cmdThingAtPointMoreInfo = VimCommand "cmdThingAtPointMoreInfo" $ \map' -> do
file <- requireArg map' "file"
line <- lookupAndReadFail map' "line"
Expand Down
25 changes: 19 additions & 6 deletions vim_runtime_path/autoload/haskellcomplete.vim
Expand Up @@ -37,12 +37,25 @@ endfunction
if !exists('g:haskellcompleteAll')
let g:haskellcompleteAll='' " '' or '-all' '-all' means complete from the set of all function exported by all modules found in all used packages
endif
"function! haskellcomplete#CompleteIdentifier(findstart, base)
" return haskellcomplete#CompleteWhat(a:findstart, a:base, 'identifier'.g:haskellcompleteAll)
"endfunction
"function! haskellcomplete#CompleteModule(findstart, base)
" return haskellcomplete#CompleteWhat(a:findstart, a:base, 'module')
"endfunction

fun! haskellcomplete#CompletModule(findstart, base)
if a:findstart
let [bc,ac] = haskellcomplete#BcAc()
return len(bc)-len(matchstr(bc,'\S*$'))
else
let [bc,ac] = haskellcomplete#BcAc()
let addImport = bc !~ 'import\s\+\S*$'
let matches = haskellcomplete#EvalScion(
\ { 'request' : 'cmdModuleCompletion'
\ , 'camelCase' : 'True'
\ , 'short' : a:base
\ })
if addImport
call map(matches, string('import ').'.v:val')
endif
return matches
endif
endf

" example: echo haskellcomplete#EvalScion({'request' : 'cmdConnectionInfo', 'file' : 'test.hs'})
function! haskellcomplete#EvalScion(request)
Expand Down

0 comments on commit fd9079b

Please sign in to comment.