diff --git a/tree-sitter/ChangeLog.md b/tree-sitter/ChangeLog.md index b694e5fe..1306fe22 100644 --- a/tree-sitter/ChangeLog.md +++ b/tree-sitter/ChangeLog.md @@ -1,3 +1,7 @@ +### v0.9.0.4 + +* Add bindings for the edit API. + ### v0.9.0.3 * Support `base-4.14` at least (GHC 8.10.7) diff --git a/tree-sitter/src/TreeSitter/Node.hs b/tree-sitter/src/TreeSitter/Node.hs index fec620aa..da347c30 100644 --- a/tree-sitter/src/TreeSitter/Node.hs +++ b/tree-sitter/src/TreeSitter/Node.hs @@ -9,6 +9,10 @@ module TreeSitter.Node , FieldId(..) , ts_node_copy_child_nodes , ts_node_poke_p + +, evalStruct +, peekStruct +, pokeStruct ) where import Foreign diff --git a/tree-sitter/src/TreeSitter/Tree.hs b/tree-sitter/src/TreeSitter/Tree.hs index 48f187d2..25f06566 100644 --- a/tree-sitter/src/TreeSitter/Tree.hs +++ b/tree-sitter/src/TreeSitter/Tree.hs @@ -1,12 +1,16 @@ +{-# LANGUAGE DeriveGeneric #-} module TreeSitter.Tree ( Tree +, TSInputEdit(..) , withRootNode +, ts_tree_edit , ts_tree_delete , ts_tree_root_node_p ) where import Foreign import TreeSitter.Node +import GHC.Generics -- | This type is uninhabited and used only for type safety within 'Ptr' values. data Tree @@ -16,5 +20,37 @@ withRootNode tree action = alloca $ \ ptr -> do ts_tree_root_node_p tree ptr action ptr +-- | Locational info used for to adjust the source ranges of a 'Tree'\'s nodes. +-- This record dirrectly corresponds to the C struct of the same name. +data TSInputEdit = TSInputEdit + { start_byte :: !Word32 + , old_end_byte :: !Word32 + , new_end_byte :: !Word32 + , start_point :: !TSPoint + , old_end_point :: !TSPoint + , new_end_point :: !TSPoint + } + deriving (Show, Eq, Generic) + +instance Storable TSInputEdit where + alignment _ = alignment (0 :: Int32) + sizeOf _ = 36 + peek = evalStruct $ + TSInputEdit <$> peekStruct + <*> peekStruct + <*> peekStruct + <*> peekStruct + <*> peekStruct + <*> peekStruct + poke ptr (TSInputEdit sb oldEb newEb sp oldEp newEp) = + flip evalStruct ptr $ do + pokeStruct sb + pokeStruct oldEb + pokeStruct newEb + pokeStruct sp + pokeStruct oldEp + pokeStruct newEp + +foreign import ccall safe "ts_tree_edit" ts_tree_edit :: Ptr Tree -> Ptr TSInputEdit -> IO () foreign import ccall safe "ts_tree_delete" ts_tree_delete :: Ptr Tree -> IO () foreign import ccall unsafe "src/bridge.c ts_tree_root_node_p" ts_tree_root_node_p :: Ptr Tree -> Ptr Node -> IO ()