Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fmt): libs::offset_to_position #3447

Merged
merged 1 commit into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions prisma-fmt/src/code_actions/relations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ fn create_missing_unique<'a>(

let new_text = format!("{separator}{indentation}@@unique([{fields}]){newline}}}");

let start = crate::offset_to_position(model.ast_model().span().end - 1, schema).unwrap();
let end = crate::offset_to_position(model.ast_model().span().end, schema).unwrap();
let start = crate::offset_to_position(model.ast_model().span().end - 1, schema);
let end = crate::offset_to_position(model.ast_model().span().end, schema);

let range = Range { start, end };

Expand Down
8 changes: 4 additions & 4 deletions prisma-fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,18 @@ pub(crate) fn range_to_span(range: Range, document: &str) -> ast::Span {

/// Gives the LSP position right after the given span.
pub(crate) fn position_after_span(span: ast::Span, document: &str) -> Position {
offset_to_position(span.end - 1, document).unwrap()
offset_to_position(span.end - 1, document)
}

/// Converts a byte offset to an LSP position, if the given offset
/// does not overflow the document.
pub(crate) fn offset_to_position(offset: usize, document: &str) -> Option<Position> {
pub(crate) fn offset_to_position(offset: usize, document: &str) -> Position {
let mut position = Position::default();

for (i, chr) in document.chars().enumerate() {
match chr {
_ if i == offset => {
return Some(position);
return position;
}
'\n' => {
position.character = 0;
Expand All @@ -191,7 +191,7 @@ pub(crate) fn offset_to_position(offset: usize, document: &str) -> Option<Positi
}
}

None
position
}

#[cfg(test)]
Expand Down