Skip to content

Commit

Permalink
refactor(syntax): move syntax kind helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Dec 14, 2024
1 parent a2a1785 commit cb2a405
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
10 changes: 4 additions & 6 deletions crates/formatter/src/printer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,10 @@ where
return vec![];
}
let mut docs = Vec::with_capacity(3);
if trivias.first().is_some_and(|token| {
matches!(
token.kind(),
SyntaxKind::LINE_COMMENT | SyntaxKind::BLOCK_COMMENT
)
}) {
if trivias
.first()
.is_some_and(|token| token.kind().is_comment())
{
docs.push(Doc::soft_line());
}
trivias.iter().for_each(|token| match token.kind() {
Expand Down
7 changes: 1 addition & 6 deletions crates/service/src/features/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,12 +754,7 @@ fn find_leading_l_paren(token: &SyntaxToken) -> Option<SyntaxToken> {
token
.siblings_with_tokens(Direction::Prev)
.skip(1)
.skip_while(|element| {
matches!(
element.kind(),
SyntaxKind::WHITESPACE | SyntaxKind::LINE_COMMENT | SyntaxKind::BLOCK_COMMENT
)
})
.skip_while(|element| element.kind().is_trivia())
.find_map(SyntaxElement::into_token)
.filter(is_l_paren)
}
Expand Down
6 changes: 3 additions & 3 deletions crates/service/src/features/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(crate) use self::semantic_tokens::SemanticTokenKind;
use crate::{files::FilesCtx, helpers, InternUri, LanguageService};
use lsp_types::Position;
use rowan::TokenAtOffset;
use wat_syntax::{is_punc, is_trivia, SyntaxNode, SyntaxToken};
use wat_syntax::{SyntaxNode, SyntaxToken};

fn find_meaningful_token(
service: &LanguageService,
Expand All @@ -31,8 +31,8 @@ fn find_meaningful_token(
TokenAtOffset::None => None,
TokenAtOffset::Single(token) => Some(token),
TokenAtOffset::Between(left, right) => {
let left_check = is_trivia(&left) || is_punc(&left);
let right_check = is_trivia(&right) || is_punc(&right);
let left_check = left.kind().is_trivia() || left.kind().is_punc();
let right_check = right.kind().is_trivia() || right.kind().is_punc();
if left_check && right_check || !left_check && !right_check {
None
} else if left_check {
Expand Down
22 changes: 22 additions & 0 deletions crates/syntax/src/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,28 @@ pub enum SyntaxKind {
ROOT,
}

impl SyntaxKind {
#[inline]
/// Checks if it is whitespace or comment.
pub fn is_trivia(self) -> bool {
matches!(
self,
SyntaxKind::WHITESPACE | SyntaxKind::LINE_COMMENT | SyntaxKind::BLOCK_COMMENT
)
}

#[inline]
pub fn is_comment(self) -> bool {
matches!(self, SyntaxKind::LINE_COMMENT | SyntaxKind::BLOCK_COMMENT)
}

#[inline]
/// Checks if it is punctuation.
pub fn is_punc(self) -> bool {
matches!(self, SyntaxKind::L_PAREN | SyntaxKind::R_PAREN)
}
}

impl From<SyntaxKind> for rowan::SyntaxKind {
fn from(kind: SyntaxKind) -> Self {
Self(kind as u16)
Expand Down
15 changes: 0 additions & 15 deletions crates/syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,3 @@ impl rowan::Language for WatLanguage {
pub type SyntaxNode = rowan::SyntaxNode<WatLanguage>;
pub type SyntaxToken = rowan::SyntaxToken<WatLanguage>;
pub type SyntaxElement = rowan::SyntaxElement<WatLanguage>;

#[inline]
/// Checks if a token is whitespace or comment.
pub fn is_trivia(token: &SyntaxToken) -> bool {
matches!(
token.kind(),
SyntaxKind::WHITESPACE | SyntaxKind::LINE_COMMENT | SyntaxKind::BLOCK_COMMENT
)
}

#[inline]
/// Checks if a token is punctuation.
pub fn is_punc(token: &SyntaxToken) -> bool {
matches!(token.kind(), SyntaxKind::L_PAREN | SyntaxKind::R_PAREN)
}

0 comments on commit cb2a405

Please sign in to comment.