-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Tree-sitter support for advanced highlighting
Fixes #398 Add Tree-sitter support for advanced syntax highlighting for Python files. * **New File**: `src/biscuit/editor/text/treesitter.py` - Implement Tree-sitter highlighter for Python files. - Load language library and extract highlight information from the syntax tree. * **Update**: `src/biscuit/editor/text/highlighter.py` - Import and initialize Tree-sitter highlighter for Python files. - Add methods to highlight text content using Tree-sitter and Pygments as a fallback. * **Update**: `src/biscuit/editor/editor.py` - Import the Highlighter class. * **Update**: `README.md` - Add documentation for Tree-sitter highlighter and Pygments fallback. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/tomlin7/biscuit/issues/398?shareId=XXXX-XXXX-XXXX-XXXX).
- Loading branch information
Showing
4 changed files
with
72 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import tree_sitter | ||
from tree_sitter import Language, Parser | ||
|
||
class TreeSitterHighlighter: | ||
def __init__(self, language: str): | ||
self.language = language | ||
self.parser = Parser() | ||
self.language_library = self.load_language_library(language) | ||
self.parser.set_language(self.language_library) | ||
|
||
def load_language_library(self, language: str): | ||
# Load the language library for the given language | ||
if language == "python": | ||
return Language('build/my-languages.so', 'python') | ||
else: | ||
raise ValueError(f"Unsupported language: {language}") | ||
|
||
def highlight(self, code: str): | ||
tree = self.parser.parse(bytes(code, "utf8")) | ||
root_node = tree.root_node | ||
return self.extract_highlight_info(root_node) | ||
|
||
def extract_highlight_info(self, node): | ||
# Extract highlight information from the syntax tree | ||
highlight_info = [] | ||
for child in node.children: | ||
highlight_info.append({ | ||
"type": child.type, | ||
"start_byte": child.start_byte, | ||
"end_byte": child.end_byte, | ||
"start_point": child.start_point, | ||
"end_point": child.end_point | ||
}) | ||
highlight_info.extend(self.extract_highlight_info(child)) | ||
return highlight_info |