feat: add the semantic version to TSLanguage, and expose an API for retrieving it
This commit is contained in:
parent
f0222107b8
commit
8bb1448a6f
24 changed files with 371 additions and 77 deletions
|
|
@ -180,6 +180,14 @@ pub struct TSQueryCursorOptions {
|
|||
pub progress_callback:
|
||||
::core::option::Option<unsafe extern "C" fn(state: *mut TSQueryCursorState) -> bool>,
|
||||
}
|
||||
#[doc = " The metadata associated with a language.\n\n Currently, this metadata can be used to check the [Semantic Version](https://semver.org/)\n of the language. This version information should be used to signal if a given parser might\n be incompatible with existing queries when upgrading between major versions, or minor versions\n if it's in zerover."]
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct TSLanguageMetadata {
|
||||
pub major_version: u8,
|
||||
pub minor_version: u8,
|
||||
pub patch_version: u8,
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Create a new parser."]
|
||||
pub fn ts_parser_new() -> *mut TSParser;
|
||||
|
|
@ -193,7 +201,7 @@ extern "C" {
|
|||
pub fn ts_parser_language(self_: *const TSParser) -> *const TSLanguage;
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Set the language that the parser should use for parsing.\n\n Returns a boolean indicating whether or not the language was successfully\n assigned. True means assignment succeeded. False means there was a version\n mismatch: the language was generated with an incompatible version of the\n Tree-sitter CLI. Check the language's version using [`ts_language_version`]\n and compare it to this library's [`TREE_SITTER_LANGUAGE_VERSION`] and\n [`TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION`] constants."]
|
||||
#[doc = " Set the language that the parser should use for parsing.\n\n Returns a boolean indicating whether or not the language was successfully\n assigned. True means assignment succeeded. False means there was a version\n mismatch: the language was generated with an incompatible version of the\n Tree-sitter CLI. Check the language's ABI version using [`ts_language_abi_version`]\n and compare it to this library's [`TREE_SITTER_LANGUAGE_VERSION`] and\n [`TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION`] constants."]
|
||||
pub fn ts_parser_set_language(self_: *mut TSParser, language: *const TSLanguage) -> bool;
|
||||
}
|
||||
extern "C" {
|
||||
|
|
@ -807,9 +815,17 @@ extern "C" {
|
|||
pub fn ts_language_symbol_type(self_: *const TSLanguage, symbol: TSSymbol) -> TSSymbolType;
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Get the ABI version number for this language. This version number is used\n to ensure that languages were generated by a compatible version of\n Tree-sitter.\n\n See also [`ts_parser_set_language`]."]
|
||||
#[doc = " @deprecated use [`ts_language_abi_version`] instead, this will be removed in 0.26.\n\n Get the ABI version number for this language. This version number is used\n to ensure that languages were generated by a compatible version of\n Tree-sitter.\n\n See also [`ts_parser_set_language`]."]
|
||||
pub fn ts_language_version(self_: *const TSLanguage) -> u32;
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Get the ABI version number for this language. This version number is used\n to ensure that languages were generated by a compatible version of\n Tree-sitter.\n\n See also [`ts_parser_set_language`]."]
|
||||
pub fn ts_language_abi_version(self_: *const TSLanguage) -> u32;
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Get the metadata for this language. This information is generated by the\n CLI, and relies on the language author providing the correct metadata in\n the language's `tree-sitter.json` file.\n\n See also [`TSMetadata`]."]
|
||||
pub fn ts_language_metadata(self_: *const TSLanguage) -> *const TSLanguageMetadata;
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Get the next parse state. Combine this with lookahead iterators to generate\n completion suggestions or valid symbols in error nodes. Use\n [`ts_node_grammar_symbol`] for valid symbols."]
|
||||
pub fn ts_language_next_state(
|
||||
|
|
|
|||
|
|
@ -64,6 +64,29 @@ pub struct Language(*const ffi::TSLanguage);
|
|||
|
||||
pub struct LanguageRef<'a>(*const ffi::TSLanguage, PhantomData<&'a ()>);
|
||||
|
||||
/// The metadata associated with a language.
|
||||
///
|
||||
/// Currently, this metadata can be used to check the [Semantic Version](https://semver.org/)
|
||||
/// of the language. This version information should be used to signal if a given parser might
|
||||
/// be incompatible with existing queries when upgrading between major versions, or minor versions
|
||||
/// if it's in zerover.
|
||||
#[doc(alias = "TSLanguageMetadata")]
|
||||
pub struct LanguageMetadata {
|
||||
pub major_version: u8,
|
||||
pub minor_version: u8,
|
||||
pub patch_version: u8,
|
||||
}
|
||||
|
||||
impl From<ffi::TSLanguageMetadata> for LanguageMetadata {
|
||||
fn from(val: ffi::TSLanguageMetadata) -> Self {
|
||||
Self {
|
||||
major_version: val.major_version,
|
||||
minor_version: val.minor_version,
|
||||
patch_version: val.patch_version,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A tree that represents the syntactic structure of a source code file.
|
||||
#[doc(alias = "TSTree")]
|
||||
pub struct Tree(NonNull<ffi::TSTree>);
|
||||
|
|
@ -394,7 +417,7 @@ impl Language {
|
|||
}
|
||||
|
||||
/// Get the name of this language. This returns `None` in older parsers.
|
||||
#[doc(alias = "ts_language_version")]
|
||||
#[doc(alias = "ts_language_name")]
|
||||
#[must_use]
|
||||
pub fn name(&self) -> Option<&'static str> {
|
||||
let ptr = unsafe { ffi::ts_language_name(self.0) };
|
||||
|
|
@ -404,11 +427,34 @@ impl Language {
|
|||
/// Get the ABI version number that indicates which version of the
|
||||
/// Tree-sitter CLI that was used to generate this [`Language`].
|
||||
#[doc(alias = "ts_language_version")]
|
||||
#[deprecated(since = "0.25.0", note = "Use abi_version instead")]
|
||||
#[must_use]
|
||||
pub fn version(&self) -> usize {
|
||||
unsafe { ffi::ts_language_version(self.0) as usize }
|
||||
}
|
||||
|
||||
/// Get the ABI version number that indicates which version of the
|
||||
/// Tree-sitter CLI that was used to generate this [`Language`].
|
||||
#[doc(alias = "ts_language_abi_version")]
|
||||
#[must_use]
|
||||
pub fn abi_version(&self) -> usize {
|
||||
unsafe { ffi::ts_language_abi_version(self.0) as usize }
|
||||
}
|
||||
|
||||
/// Get the metadata for this language. This information is generated by the
|
||||
/// CLI, and relies on the language author providing the correct metadata in
|
||||
/// the language's `tree-sitter.json` file.
|
||||
///
|
||||
/// See also [`LanguageMetadata`].
|
||||
#[doc(alias = "ts_language_metadata")]
|
||||
#[must_use]
|
||||
pub fn metadata(&self) -> Option<LanguageMetadata> {
|
||||
unsafe {
|
||||
let ptr = ffi::ts_language_metadata(self.0);
|
||||
(!ptr.is_null()).then(|| (*ptr).into())
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the number of distinct node types in this language.
|
||||
#[doc(alias = "ts_language_symbol_count")]
|
||||
#[must_use]
|
||||
|
|
@ -613,7 +659,7 @@ impl Parser {
|
|||
/// [`LANGUAGE_VERSION`] and [`MIN_COMPATIBLE_LANGUAGE_VERSION`] constants.
|
||||
#[doc(alias = "ts_parser_set_language")]
|
||||
pub fn set_language(&mut self, language: &Language) -> Result<(), LanguageError> {
|
||||
let version = language.version();
|
||||
let version = language.abi_version();
|
||||
if (MIN_COMPATIBLE_LANGUAGE_VERSION..=LANGUAGE_VERSION).contains(&version) {
|
||||
unsafe {
|
||||
ffi::ts_parser_set_language(self.0.as_ptr(), language.0);
|
||||
|
|
@ -2360,7 +2406,7 @@ impl Query {
|
|||
column: 0,
|
||||
offset: 0,
|
||||
message: LanguageError {
|
||||
version: language.version(),
|
||||
version: language.abi_version(),
|
||||
}
|
||||
.to_string(),
|
||||
kind: QueryErrorKind::Language,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue