chore(tags): apply clippy fixes

This commit is contained in:
Amaan Qureshi 2024-02-04 02:00:48 -05:00
parent f32fa784be
commit 62b3794838
No known key found for this signature in database
GPG key ID: E67890ADC4227273
2 changed files with 56 additions and 56 deletions

View file

@ -66,17 +66,17 @@ pub extern "C" fn ts_tagger_new() -> *mut TSTagger {
}))
}
/// Delete a TSTagger.
/// Delete a [`TSTagger`].
///
/// # Safety
///
/// `this` must be non-null and a valid pointer to a [`TSTagger`] instance.
#[no_mangle]
pub unsafe extern "C" fn ts_tagger_delete(this: *mut TSTagger) {
drop(Box::from_raw(this))
drop(Box::from_raw(this));
}
/// Add a language to a TSTagger.
/// Add a language to a [`TSTagger`].
///
/// Returns a [`TSTagsError`] indicating whether the operation was successful or not.
///
@ -105,13 +105,11 @@ pub unsafe extern "C" fn ts_tagger_add_language(
} else {
&[]
};
let tags_query = match str::from_utf8(tags_query) {
Ok(e) => e,
Err(_) => return TSTagsError::InvalidUtf8,
let Ok(tags_query) = str::from_utf8(tags_query) else {
return TSTagsError::InvalidUtf8;
};
let locals_query = match str::from_utf8(locals_query) {
Ok(e) => e,
Err(_) => return TSTagsError::InvalidUtf8,
let Ok(locals_query) = str::from_utf8(locals_query) else {
return TSTagsError::InvalidUtf8;
};
match TagsConfiguration::new(language, tags_query, locals_query) {
@ -169,16 +167,13 @@ pub unsafe extern "C" fn ts_tagger_tag(
Err(e) => {
return match e {
Error::InvalidLanguage => TSTagsError::InvalidLanguage,
Error::Cancelled => TSTagsError::Timeout,
_ => TSTagsError::Timeout,
}
}
};
for tag in tags {
let tag = if let Ok(tag) = tag {
tag
} else {
let Ok(tag) = tag else {
buffer.tags.clear();
buffer.docs.clear();
return TSTagsError::Timeout;
@ -228,7 +223,7 @@ pub extern "C" fn ts_tags_buffer_new() -> *mut TSTagsBuffer {
}))
}
/// Delete a TSTagsBuffer.
/// Delete a [`TSTagsBuffer`].
///
/// # Safety
///
@ -236,10 +231,10 @@ pub extern "C" fn ts_tags_buffer_new() -> *mut TSTagsBuffer {
/// [`ts_tags_buffer_new`].
#[no_mangle]
pub unsafe extern "C" fn ts_tags_buffer_delete(this: *mut TSTagsBuffer) {
drop(Box::from_raw(this))
drop(Box::from_raw(this));
}
/// Get the tags from a TSTagsBuffer.
/// Get the tags from a [`TSTagsBuffer`].
///
/// # Safety
///
@ -250,22 +245,20 @@ pub unsafe extern "C" fn ts_tags_buffer_delete(this: *mut TSTagsBuffer) {
/// is deleted with [`ts_tags_buffer_delete`], else the data will point to garbage.
#[no_mangle]
pub unsafe extern "C" fn ts_tags_buffer_tags(this: *const TSTagsBuffer) -> *const TSTag {
let buffer = unwrap_ptr(this);
buffer.tags.as_ptr()
unwrap_ptr(this).tags.as_ptr()
}
/// Get the number of tags in a TSTagsBuffer.
/// Get the number of tags in a [`TSTagsBuffer`].
///
/// # Safety
///
/// `this` must be non-null and a valid pointer to a [`TSTagsBuffer`] instance.
#[no_mangle]
pub unsafe extern "C" fn ts_tags_buffer_tags_len(this: *const TSTagsBuffer) -> u32 {
let buffer = unwrap_ptr(this);
buffer.tags.len() as u32
unwrap_ptr(this).tags.len() as u32
}
/// Get the documentation strings from a TSTagsBuffer.
/// Get the documentation strings from a [`TSTagsBuffer`].
///
/// # Safety
///
@ -279,11 +272,10 @@ pub unsafe extern "C" fn ts_tags_buffer_tags_len(this: *const TSTagsBuffer) -> u
/// To get the length of the string, use [`ts_tags_buffer_docs_len`].
#[no_mangle]
pub unsafe extern "C" fn ts_tags_buffer_docs(this: *const TSTagsBuffer) -> *const c_char {
let buffer = unwrap_ptr(this);
buffer.docs.as_ptr() as *const c_char
unwrap_ptr(this).docs.as_ptr().cast::<c_char>()
}
/// Get the length of the documentation strings in a TSTagsBuffer.
/// Get the length of the documentation strings in a [`TSTagsBuffer`].
///
/// # Safety
///
@ -291,11 +283,10 @@ pub unsafe extern "C" fn ts_tags_buffer_docs(this: *const TSTagsBuffer) -> *cons
/// [`ts_tags_buffer_new`].
#[no_mangle]
pub unsafe extern "C" fn ts_tags_buffer_docs_len(this: *const TSTagsBuffer) -> u32 {
let buffer = unwrap_ptr(this);
buffer.docs.len() as u32
unwrap_ptr(this).docs.len() as u32
}
/// Get whether or not a TSTagsBuffer contains any parse errors.
/// Get whether or not a [`TSTagsBuffer`] contains any parse errors.
///
/// # Safety
///
@ -303,8 +294,7 @@ pub unsafe extern "C" fn ts_tags_buffer_docs_len(this: *const TSTagsBuffer) -> u
/// [`ts_tags_buffer_new`].
#[no_mangle]
pub unsafe extern "C" fn ts_tags_buffer_found_parse_error(this: *const TSTagsBuffer) -> bool {
let buffer = unwrap_ptr(this);
buffer.errors_present
unwrap_ptr(this).errors_present
}
/// Get the syntax kinds for a given scope name.
@ -335,7 +325,7 @@ pub unsafe extern "C" fn ts_tagger_syntax_kinds_for_scope_name(
*len = 0;
if let Some(config) = tagger.languages.get(scope_name) {
*len = config.c_syntax_type_names.len() as u32;
return config.c_syntax_type_names.as_ptr() as *const *const c_char;
return config.c_syntax_type_names.as_ptr().cast::<*const c_char>();
}
std::ptr::null()
}
@ -356,7 +346,7 @@ unsafe fn unwrap_mut_ptr<'a, T>(result: *mut T) -> &'a mut T {
fn unwrap<T, E: fmt::Display>(result: Result<T, E>) -> T {
result.unwrap_or_else(|error| {
eprintln!("tree-sitter tag error: {}", error);
eprintln!("tree-sitter tag error: {error}");
abort();
})
}

View file

@ -117,7 +117,7 @@ struct LineInfo {
impl TagsConfiguration {
pub fn new(language: Language, tags_query: &str, locals_query: &str) -> Result<Self, Error> {
let query = Query::new(&language, &format!("{}{}", locals_query, tags_query))?;
let query = Query::new(&language, &format!("{locals_query}{tags_query}"))?;
let tags_query_offset = locals_query.len();
let mut tags_pattern_index = 0;
@ -137,13 +137,12 @@ impl TagsConfiguration {
let mut local_definition_capture_index = None;
for (i, name) in query.capture_names().iter().enumerate() {
match *name {
"" => continue,
"name" => name_capture_index = Some(i as u32),
"ignore" => ignore_capture_index = Some(i as u32),
"doc" => doc_capture_index = Some(i as u32),
"local.scope" => local_scope_capture_index = Some(i as u32),
"local.definition" => local_definition_capture_index = Some(i as u32),
"local.reference" => continue,
"local.reference" | "" => continue,
_ => {
let mut is_definition = false;
@ -153,7 +152,7 @@ impl TagsConfiguration {
} else if name.starts_with("reference.") {
name.trim_start_matches("reference.")
} else {
return Err(Error::InvalidCapture(name.to_string()));
return Err(Error::InvalidCapture((*name).to_string()));
};
if let Ok(cstr) = CString::new(kind) {
@ -200,7 +199,7 @@ impl TagsConfiguration {
}
if let Some(doc_capture_index) = doc_capture_index {
for predicate in query.general_predicates(pattern_index) {
if predicate.args.get(0)
if predicate.args.first()
== Some(&QueryPredicateArg::Capture(doc_capture_index))
{
match (predicate.operator.as_ref(), predicate.args.get(1)) {
@ -216,11 +215,11 @@ impl TagsConfiguration {
}
}
}
return Ok(info);
Ok(info)
})
.collect::<Result<Vec<_>, Error>>()?;
Ok(TagsConfiguration {
Ok(Self {
language,
query,
syntax_type_names,
@ -229,26 +228,37 @@ impl TagsConfiguration {
doc_capture_index,
name_capture_index,
ignore_capture_index,
tags_pattern_index,
local_scope_capture_index,
local_definition_capture_index,
tags_pattern_index,
pattern_info,
})
}
#[must_use]
pub fn syntax_type_name(&self, id: u32) -> &str {
unsafe {
let cstr =
CStr::from_ptr(self.syntax_type_names[id as usize].as_ptr() as *const c_char)
.to_bytes();
let cstr = CStr::from_ptr(
self.syntax_type_names[id as usize]
.as_ptr()
.cast::<c_char>(),
)
.to_bytes();
str::from_utf8(cstr).expect("syntax type name was not valid utf-8")
}
}
}
impl Default for TagsContext {
fn default() -> Self {
Self::new()
}
}
impl TagsContext {
#[must_use]
pub fn new() -> Self {
TagsContext {
Self {
parser: Parser::new(),
cursor: QueryCursor::new(),
}
@ -327,9 +337,8 @@ where
let tag = self.tag_queue.remove(0).0;
if tag.is_ignored() {
continue;
} else {
return Some(Ok(tag));
}
return Some(Ok(tag));
}
}
@ -452,11 +461,10 @@ where
for doc_node in &doc_nodes[docs_start_index..] {
if let Ok(content) = str::from_utf8(&self.source[doc_node.byte_range()])
{
let content = if let Some(regex) = &pattern_info.doc_strip_regex {
regex.replace_all(content, "").to_string()
} else {
content.to_string()
};
let content = pattern_info.doc_strip_regex.as_ref().map_or_else(
|| content.to_string(),
|regex| regex.replace_all(content, "").to_string(),
);
match &mut docs {
None => docs = Some(content),
Some(d) => {
@ -511,11 +519,11 @@ where
line_range: line_range.clone(),
});
tag = Tag {
range,
name_range,
line_range,
span,
utf16_column_range,
range,
name_range,
docs,
is_definition,
syntax_type_id,
@ -554,8 +562,9 @@ where
}
impl Tag {
fn ignored(name_range: Range<usize>) -> Self {
Tag {
#[must_use]
const fn ignored(name_range: Range<usize>) -> Self {
Self {
name_range,
line_range: 0..0,
span: Point::new(0, 0)..Point::new(0, 0),
@ -567,7 +576,8 @@ impl Tag {
}
}
fn is_ignored(&self) -> bool {
#[must_use]
const fn is_ignored(&self) -> bool {
self.range.start == usize::MAX
}
}