Skip to content

Commit

Permalink
docs(swiftide): documented file swiftide/src/integrations/treesitter/…
Browse files Browse the repository at this point in the history
…supported_languages.rs (#26)

Co-authored-by: bosun-ai[bot] <157630444+bosun-ai[bot]@users.noreply.github.com>
Co-authored-by: Timon Vonk <[email protected]>
  • Loading branch information
bosun-ai[bot] and timonv authored Jun 13, 2024
1 parent 14e24c3 commit 502939f
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions swiftide/src/integrations/treesitter/supported_languages.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
//! This module defines the supported programming languages for the Swiftide project and provides utility functions
//! for mapping these languages to their respective file extensions and tree-sitter language objects.
//!
//! The primary purpose of this module is to facilitate the recognition and handling of different programming languages
//! by mapping file extensions and converting language enums to tree-sitter language objects for accurate parsing and syntax analysis.
//!
//! # Supported Languages
//! - Rust
//! - Typescript
//! - Python
//! - Ruby
//! - Javascript
#[allow(unused_imports)]
pub use std::str::FromStr as _;

/// Enum representing the supported programming languages in the Swiftide project.
///
/// This enum is used to map programming languages to their respective file extensions and tree-sitter language objects.
/// The `EnumString` and `Display` macros from the `strum_macros` crate are used to provide string conversion capabilities.
/// The `ascii_case_insensitive` attribute allows for case-insensitive string matching.
#[derive(Debug, PartialEq, Clone, Copy, strum_macros::EnumString, strum_macros::Display)]
#[strum(ascii_case_insensitive)]
pub enum SupportedLanguages {
Expand All @@ -11,13 +29,26 @@ pub enum SupportedLanguages {
Javascript,
}

/// Static array of file extensions for Rust files.
static RUST_EXTENSIONS: &[&str] = &["rs"];

/// Static array of file extensions for Typescript files.
static TYPESCRIPT_EXTENSIONS: &[&str] = &["ts", "tsx", "js", "jsx"];

/// Static array of file extensions for Python files.
static PYTHON_EXTENSIONS: &[&str] = &["py"];

/// Static array of file extensions for Ruby files.
static RUBY_EXTENSIONS: &[&str] = &["rb"];

/// Static array of file extensions for Javascript files.
static JAVASCRIPT_EXTENSIONS: &[&str] = &["js", "jsx"];

impl SupportedLanguages {
/// Returns the file extensions associated with the supported language.
///
/// # Returns
/// A static slice of string slices representing the file extensions.
pub fn file_extensions(&self) -> &[&str] {
match self {
SupportedLanguages::Rust => RUST_EXTENSIONS,
Expand All @@ -30,6 +61,16 @@ impl SupportedLanguages {
}

impl From<SupportedLanguages> for tree_sitter::Language {
/// Converts a `SupportedLanguages` enum to a `tree_sitter::Language` object.
///
/// This implementation allows for the conversion of the supported languages to their respective tree-sitter language objects,
/// enabling accurate parsing and syntax analysis.
///
/// # Parameters
/// - `val`: The `SupportedLanguages` enum value to be converted.
///
/// # Returns
/// A `tree_sitter::Language` object corresponding to the provided `SupportedLanguages` enum value.
fn from(val: SupportedLanguages) -> Self {
match val {
SupportedLanguages::Rust => tree_sitter_rust::language(),
Expand All @@ -45,6 +86,7 @@ impl From<SupportedLanguages> for tree_sitter::Language {
mod test {
use super::*;

/// Tests the case-insensitive string conversion for `SupportedLanguages`.
#[test]
fn test_supported_languages_from_str() {
assert_eq!(
Expand All @@ -57,6 +99,7 @@ mod test {
);
}

/// Tests the case-insensitive string conversion for `SupportedLanguages` with different casing.
#[test]
fn test_supported_languages_from_str_case_insensitive() {
assert_eq!(
Expand Down

0 comments on commit 502939f

Please sign in to comment.