3 | module Language.LSP.Utils
9 | import Language.LSP.Message
15 | headerLineEnd : String
16 | headerLineEnd = if isWindows then "\n" else "\r\n"
21 | fGetHeader : (handle : File) -> IO (Either FileError String)
22 | fGetHeader handle = do
23 | False <- fEOF handle
24 | | True => exitWith (ExitFailure 1)
25 | Right l <- fGetLine handle
26 | | Left err => pure $
Left err
29 | if isSuffixOf headerLineEnd l
31 | else (map (l ++)) <$> fGetHeader handle
35 | b16ToHexString : Bits16 -> String
54 | other => assert_total $
55 | b16ToHexString (n `shiftR` 4) ++
56 | b16ToHexString (n .&. 15)
60 | pad4 : String -> String
74 | encodeCodepointH : Bits32 -> Either String (String, String)
75 | encodeCodepointH x =
78 | True => Left $
pad4 (b16ToHexString (cast x))
81 | let x' = x - 0x10000 in
83 | ( pad4 (b16ToHexString (cast $
0xD800 + (x' `shiftR` 10)))
84 | , pad4 (b16ToHexString (cast $
0xDC00 + (x' .&. 0b1111111111))))
89 | encodeCodepoint : Bits32 -> String
91 | case encodeCodepointH x of
92 | Left w => "\\u" ++ w
93 | Right (w1, w2) => "\\u" ++ w1 ++ "\\u" ++ w2
100 | showChar : Char -> String
110 | c => if isControl c || c >= '\127'
111 | then encodeCodepoint (cast $
ord c)
115 | showString : String -> String
116 | showString x = "\"" ++ concatMap showChar (unpack x) ++ "\""
119 | stringify : JSON -> String
120 | stringify JNull = "null"
121 | stringify (JBoolean x) = if x then "true" else "false"
122 | stringify (JNumber x) =
124 | in if isSuffixOf ".0" s then substr 0 (length s `minus` 2) s else s
125 | stringify (JString x) = showString x
126 | stringify (JArray xs) = "[" ++ stringifyValues xs ++ "]"
128 | stringifyValues : List JSON -> String
129 | stringifyValues [] = ""
130 | stringifyValues (x :: xs) =
131 | stringify x ++ if isNil xs then "" else "," ++ stringifyValues xs
132 | stringify (JObject xs) = "{" ++ stringifyProps xs ++ "}"
134 | stringifyProp : (String, JSON) -> String
135 | stringifyProp (key, value) = showString key ++ ":" ++ stringify value
137 | stringifyProps : List (String, JSON) -> String
138 | stringifyProps [] = ""
139 | stringifyProps (x :: xs) =
140 | stringifyProp x ++ if isNil xs then "" else "," ++ stringifyProps xs
143 | pathToURI : String -> URI
145 | MkURI { scheme = "file"
146 | , authority = Just (MkURIAuthority Nothing "" Nothing)
153 | Show Position where
154 | show (MkPosition line character) = "\{show line}:\{show character}"
158 | show (MkRange start end) = "\{show start} -- \{show end}"
161 | systemPathToURIPath : String -> String
162 | systemPathToURIPath p = if not isWindows then p else
163 | let p1 = fastPack (map (\c => if c == '\\' then '/' else c) (fastUnpack p))
164 | in case strUncons p1 of
165 | Just ('/', _) => p1
166 | _ => strCons '/' p1
169 | uriPathToSystemPath : String -> String
170 | uriPathToSystemPath p = if not isWindows then p else
171 | let p1 = case strUncons p of
172 | Just ('/', tail) => tail
174 | in fastPack (map (\c => if c == '/' then '\\' else c) (fastUnpack p1))