Skip to content

Commit

Permalink
feat: Allow setting included ranges on the parser
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonMatthesKDAB committed Jul 17, 2024
1 parent aa41a17 commit ce3088c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/treesitter/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ std::optional<Tree> Parser::parseString(const QString &text, const Tree *old_tre
return tree ? Tree(tree) : std::optional<Tree> {};
}

bool Parser::setIncludedRanges(const QList<Range> &ranges)
{
return ts_parser_set_included_ranges(m_parser, ranges.data(), ranges.size());
}

const TSLanguage *Parser::language() const
{
return ts_parser_language(m_parser);
Expand Down
18 changes: 18 additions & 0 deletions src/treesitter/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@

#include "core/document.h"
#include <QString>
#include <tree_sitter/api.h>
#include <vector>

struct TSParser;
struct TSLanguage;

namespace treesitter {

class Tree;
using Range = TSRange;

class Parser
{
Expand All @@ -37,6 +40,21 @@ class Parser

std::optional<Tree> parseString(const QString &text, const Tree *old_tree = nullptr) const;

/**
* Parse only the given ranges.
* Note: if the ranges are empty, the entire document is parsed.
*
* From tree_sitter/api.h:
* [The] given ranges must be ordered from earliest to latest in the document,
* and they must not overlap. That is, the following must hold for all
* `i` < `length - 1`: ranges[i].end_byte <= ranges[i + 1].start_byte
*
* If this requirement is not satisfied, the operation will fail, the ranges
* will not be assigned, and this function will return `false`. On success,
* this function returns `true`
*/
bool setIncludedRanges(const QList<Range> &ranges);

const TSLanguage *language() const;

static TSLanguage *getLanguage(Core::Document::Type type);
Expand Down

0 comments on commit ce3088c

Please sign in to comment.