|
1 |
| -module Ide.Plugin.Cabal.Completion.CabalFields (findStanzaForColumn, findFieldSection, getOptionalSectionName, getAnnotation, getFieldName, onelineSectionArgs) where |
| 1 | +{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-} |
| 2 | +module Ide.Plugin.Cabal.Completion.CabalFields (findStanzaForColumn, findFieldSection, findTextWord, findFieldLine, getOptionalSectionName, getAnnotation, getFieldName, onelineSectionArgs, getFieldEndPosition, getSectionArgEndPosition, getNameEndPosition, getFieldLineEndPosition, getFieldLSPRange) where |
2 | 3 |
|
| 4 | +import qualified Data.ByteString as BS |
| 5 | +import Data.List (find) |
3 | 6 | import Data.List.NonEmpty (NonEmpty)
|
4 | 7 | import qualified Data.List.NonEmpty as NE
|
5 | 8 | import qualified Data.Text as T
|
6 | 9 | import qualified Data.Text.Encoding as T
|
7 | 10 | import qualified Distribution.Fields as Syntax
|
8 | 11 | import qualified Distribution.Parsec.Position as Syntax
|
9 | 12 | import Ide.Plugin.Cabal.Completion.Types
|
| 13 | +import qualified Language.LSP.Protocol.Types as LSPTypes |
10 | 14 |
|
11 | 15 | -- ----------------------------------------------------------------
|
12 | 16 | -- Cabal-syntax utilities I don't really want to write myself
|
@@ -46,6 +50,71 @@ findFieldSection cursor (x:y:ys)
|
46 | 50 | where
|
47 | 51 | cursorLine = Syntax.positionRow cursor
|
48 | 52 |
|
| 53 | +-- | Determine the field line the cursor is currently a part of. |
| 54 | +-- |
| 55 | +-- The result is said field line and its starting position |
| 56 | +-- or Nothing if the passed list of fields is empty. |
| 57 | + |
| 58 | +-- This function assumes that elements in a field's @FieldLine@ list |
| 59 | +-- do not share the same row. |
| 60 | +findFieldLine :: Syntax.Position -> [Syntax.Field Syntax.Position] -> Maybe (Syntax.FieldLine Syntax.Position) |
| 61 | +findFieldLine _cursor [] = Nothing |
| 62 | +findFieldLine cursor fields = |
| 63 | + case findFieldSection cursor fields of |
| 64 | + Nothing -> Nothing |
| 65 | + Just (Syntax.Field _ fieldLines) -> find filterLineFields fieldLines |
| 66 | + Just (Syntax.Section _ _ fields) -> findFieldLine cursor fields |
| 67 | + where |
| 68 | + cursorLine = Syntax.positionRow cursor |
| 69 | + -- In contrast to `Field` or `Section`, `FieldLine` must have the exact |
| 70 | + -- same line position as the cursor. |
| 71 | + filterLineFields (Syntax.FieldLine pos _) = Syntax.positionRow pos == cursorLine |
| 72 | + |
| 73 | +-- | Determine the exact word at the current cursor position. |
| 74 | +-- |
| 75 | +-- The result is said word or Nothing if the passed list is empty |
| 76 | +-- or the cursor position is not next to, or on a word. |
| 77 | +-- For this function, a word is a sequence of consecutive characters |
| 78 | +-- that are not a space or column. |
| 79 | + |
| 80 | +-- This function currently only considers words inside of a @FieldLine@. |
| 81 | +findTextWord :: Syntax.Position -> [Syntax.Field Syntax.Position] -> Maybe T.Text |
| 82 | +findTextWord _cursor [] = Nothing |
| 83 | +findTextWord cursor fields = |
| 84 | + case findFieldLine cursor fields of |
| 85 | + Nothing -> Nothing |
| 86 | + Just (Syntax.FieldLine pos byteString) -> |
| 87 | + let decodedText = T.decodeUtf8 byteString |
| 88 | + lineFieldCol = Syntax.positionCol pos |
| 89 | + lineFieldLen = T.length decodedText |
| 90 | + offset = cursorCol - lineFieldCol in |
| 91 | + -- Range check if cursor is inside or or next to found line. |
| 92 | + -- The latter comparison includes the length of the line as offset, |
| 93 | + -- which is done to also include cursors that are at the end of a line. |
| 94 | + -- e.g. "foo,bar|" |
| 95 | + -- ^ |
| 96 | + -- cursor |
| 97 | + -- |
| 98 | + -- Having an offset which is outside of the line is possible because of `splitAt`. |
| 99 | + if offset >= 0 && lineFieldLen >= offset |
| 100 | + then |
| 101 | + let (lhs, rhs) = T.splitAt offset decodedText |
| 102 | + strippedLhs = T.takeWhileEnd isAllowedChar lhs |
| 103 | + strippedRhs = T.takeWhile isAllowedChar rhs |
| 104 | + resultText = T.concat [strippedLhs, strippedRhs] in |
| 105 | + -- It could be possible that the cursor was in-between separators, in this |
| 106 | + -- case the resulting text would be empty, which should result in `Nothing`. |
| 107 | + -- e.g. " foo ,| bar" |
| 108 | + -- ^ |
| 109 | + -- cursor |
| 110 | + if not $ T.null resultText then Just resultText else Nothing |
| 111 | + else |
| 112 | + Nothing |
| 113 | + where |
| 114 | + cursorCol = Syntax.positionCol cursor |
| 115 | + separators = [',', ' '] |
| 116 | + isAllowedChar = (`notElem` separators) |
| 117 | + |
49 | 118 | type FieldName = T.Text
|
50 | 119 |
|
51 | 120 | getAnnotation :: Syntax.Field ann -> ann
|
@@ -73,12 +142,42 @@ getOptionalSectionName (x:xs) = case x of
|
73 | 142 | --
|
74 | 143 | -- For example, @flag@ @(@ @pedantic@ @)@ will be joined in
|
75 | 144 | -- one line, instead of four @SectionArg@s separately.
|
76 |
| -onelineSectionArgs :: [Syntax.SectionArg Syntax.Position] -> T.Text |
| 145 | +onelineSectionArgs :: [Syntax.SectionArg ann] -> T.Text |
77 | 146 | onelineSectionArgs sectionArgs = joinedName
|
78 | 147 | where
|
79 | 148 | joinedName = T.unwords $ map getName sectionArgs
|
80 | 149 |
|
81 |
| - getName :: Syntax.SectionArg Syntax.Position -> T.Text |
| 150 | + getName :: Syntax.SectionArg ann -> T.Text |
82 | 151 | getName (Syntax.SecArgName _ identifier) = T.decodeUtf8 identifier
|
83 | 152 | getName (Syntax.SecArgStr _ quotedString) = T.decodeUtf8 quotedString
|
84 | 153 | getName (Syntax.SecArgOther _ string) = T.decodeUtf8 string
|
| 154 | + |
| 155 | + |
| 156 | +-- | Returns the end position of a provided field |
| 157 | +getFieldEndPosition :: Syntax.Field Syntax.Position -> Syntax.Position |
| 158 | +getFieldEndPosition (Syntax.Field name []) = getNameEndPosition name |
| 159 | +getFieldEndPosition (Syntax.Field _ (x:xs)) = getFieldLineEndPosition $ NE.last (x NE.:| xs) |
| 160 | +getFieldEndPosition (Syntax.Section name [] []) = getNameEndPosition name |
| 161 | +getFieldEndPosition (Syntax.Section _ (x:xs) []) = getSectionArgEndPosition $ NE.last (x NE.:| xs) |
| 162 | +getFieldEndPosition (Syntax.Section _ _ (x:xs)) = getFieldEndPosition $ NE.last (x NE.:| xs) |
| 163 | + |
| 164 | +-- | Returns the end position of a provided section arg |
| 165 | +getSectionArgEndPosition :: Syntax.SectionArg Syntax.Position -> Syntax.Position |
| 166 | +getSectionArgEndPosition (Syntax.SecArgName (Syntax.Position row col) byteString) = Syntax.Position row (col + BS.length byteString) |
| 167 | +getSectionArgEndPosition (Syntax.SecArgStr (Syntax.Position row col) byteString) = Syntax.Position row (col + BS.length byteString) |
| 168 | +getSectionArgEndPosition (Syntax.SecArgOther (Syntax.Position row col) byteString) = Syntax.Position row (col + BS.length byteString) |
| 169 | + |
| 170 | +-- | Returns the end position of a provided name |
| 171 | +getNameEndPosition :: Syntax.Name Syntax.Position -> Syntax.Position |
| 172 | +getNameEndPosition (Syntax.Name (Syntax.Position row col) byteString) = Syntax.Position row (col + BS.length byteString) |
| 173 | + |
| 174 | +-- | Returns the end position of a provided field line |
| 175 | +getFieldLineEndPosition :: Syntax.FieldLine Syntax.Position -> Syntax.Position |
| 176 | +getFieldLineEndPosition (Syntax.FieldLine (Syntax.Position row col) byteString) = Syntax.Position row (col + BS.length byteString) |
| 177 | + |
| 178 | +-- | Returns a LSP compatible range for a provided field |
| 179 | +getFieldLSPRange :: Syntax.Field Syntax.Position -> LSPTypes.Range |
| 180 | +getFieldLSPRange field = LSPTypes.Range startLSPPos endLSPPos |
| 181 | + where |
| 182 | + startLSPPos = cabalPositionToLSPPosition $ getAnnotation field |
| 183 | + endLSPPos = cabalPositionToLSPPosition $ getFieldEndPosition field |
0 commit comments