Skip to content

Commit

Permalink
Add ts_tree_edit (#321)
Browse files Browse the repository at this point in the history
* add ts_tree_edit

* export TSInputEdit

* document TSInputEdit

* update changelog

---------

Co-authored-by: Madeleine Sydney <[email protected]>
  • Loading branch information
crumb and msyds authored Sep 23, 2024
1 parent 961a682 commit bf614b3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tree-sitter/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 4 additions & 0 deletions tree-sitter/src/TreeSitter/Node.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ module TreeSitter.Node
, FieldId(..)
, ts_node_copy_child_nodes
, ts_node_poke_p

, evalStruct
, peekStruct
, pokeStruct
) where

import Foreign
Expand Down
36 changes: 36 additions & 0 deletions tree-sitter/src/TreeSitter/Tree.hs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 ()

0 comments on commit bf614b3

Please sign in to comment.