use std::fmt::{self, Display, Formatter};
use crate::domain::{ObjectId, RepoPath};
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum FileRevision {
WorkingTree,
Commit(ObjectId),
}
impl FileRevision {
#[must_use]
pub fn display(&self) -> String {
self.to_string()
}
}
impl Display for FileRevision {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::WorkingTree => formatter.write_str("working tree"),
Self::Commit(commit) => write!(formatter, "commit {}", commit.short()),
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum DocumentSymbolKind {
Unspecified,
File,
Module,
Namespace,
Package,
Class,
Method,
Property,
Field,
Constructor,
Enum,
Interface,
Function,
Variable,
Constant,
String,
Number,
Boolean,
Array,
Object,
Key,
Null,
EnumMember,
Struct,
Event,
Operator,
TypeParameter,
Other(u64),
}
impl DocumentSymbolKind {
#[must_use]
pub const fn label(self) -> &'static str {
match self {
Self::File => "file",
Self::Module => "module",
Self::Namespace => "namespace",
Self::Package => "package",
Self::Class => "class",
Self::Method => "method",
Self::Property => "property",
Self::Field => "field",
Self::Constructor => "constructor",
Self::Enum => "enum",
Self::Interface => "interface",
Self::Function => "function",
Self::Variable => "variable",
Self::Constant => "constant",
Self::String => "string",
Self::Number => "number",
Self::Boolean => "boolean",
Self::Array => "array",
Self::Object => "object",
Self::Key => "key",
Self::Null => "null",
Self::EnumMember => "enum member",
Self::Struct => "struct",
Self::Event => "event",
Self::Operator => "operator",
Self::TypeParameter => "type parameter",
Self::Unspecified | Self::Other(_) => "symbol",
}
}
}
impl From<u64> for DocumentSymbolKind {
fn from(value: u64) -> Self {
match value {
1 => Self::File,
2 => Self::Module,
3 => Self::Namespace,
4 => Self::Package,
5 => Self::Class,
6 => Self::Method,
7 => Self::Property,
8 => Self::Field,
9 => Self::Constructor,
10 => Self::Enum,
11 => Self::Interface,
12 => Self::Function,
13 => Self::Variable,
14 => Self::Constant,
15 => Self::String,
16 => Self::Number,
17 => Self::Boolean,
18 => Self::Array,
19 => Self::Object,
20 => Self::Key,
21 => Self::Null,
22 => Self::EnumMember,
23 => Self::Struct,
24 => Self::Event,
25 => Self::Operator,
26 => Self::TypeParameter,
value => Self::Other(value),
}
}
}
impl Display for DocumentSymbolKind {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str(self.label())
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DocumentSymbol {
name: String,
detail: Option<String>,
kind: DocumentSymbolKind,
range: SourceRange,
selection: SourcePosition,
depth: usize,
}
impl DocumentSymbol {
#[must_use]
pub fn new(
name: String,
detail: Option<String>,
kind: DocumentSymbolKind,
range: SourceRange,
selection: SourcePosition,
depth: usize,
) -> Self {
Self {
name,
detail,
kind,
range,
selection,
depth,
}
}
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
#[must_use]
pub fn detail(&self) -> Option<&str> {
self.detail.as_deref()
}
#[must_use]
pub const fn kind(&self) -> DocumentSymbolKind {
self.kind
}
#[must_use]
pub fn range(&self) -> SourceRange {
self.range
}
#[must_use]
pub fn selection(&self) -> SourcePosition {
self.selection
}
#[must_use]
pub fn depth(&self) -> usize {
self.depth
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum SemanticNavigationKind {
Definition,
Implementation,
TypeDefinition,
Declaration,
}
impl SemanticNavigationKind {
#[must_use]
pub const fn label(self) -> &'static str {
match self {
Self::Definition => "definition",
Self::Implementation => "implementation",
Self::TypeDefinition => "type definition",
Self::Declaration => "declaration",
}
}
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub struct SourcePosition {
line: u32,
byte_column: usize,
}
impl SourcePosition {
#[must_use]
pub const fn new(line: u32, byte_column: usize) -> Self {
Self { line, byte_column }
}
#[must_use]
pub const fn line(self) -> u32 {
self.line
}
#[must_use]
pub const fn byte_column(self) -> usize {
self.byte_column
}
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub struct SourceRange {
start: SourcePosition,
end: SourcePosition,
}
impl SourceRange {
#[must_use]
pub const fn new(start: SourcePosition, end: SourcePosition) -> Self {
Self { start, end }
}
#[must_use]
pub const fn start(self) -> SourcePosition {
self.start
}
#[must_use]
pub const fn end(self) -> SourcePosition {
self.end
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct RepositoryLocation {
path: RepoPath,
selection: SourceRange,
}
impl RepositoryLocation {
#[must_use]
pub fn new(path: RepoPath, selection: SourceRange) -> Self {
Self { path, selection }
}
#[must_use]
pub fn path(&self) -> &RepoPath {
&self.path
}
#[must_use]
pub const fn selection(&self) -> SourceRange {
self.selection
}
#[must_use]
pub fn into_parts(self) -> (RepoPath, SourceRange) {
(self.path, self.selection)
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum NavigationTarget {
Repository(RepositoryLocation),
External {
display_uri: String,
},
}
impl NavigationTarget {
#[must_use]
pub fn display(&self) -> String {
self.to_string()
}
}
impl Display for NavigationTarget {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::Repository(location) => write!(
formatter,
"{}:{}:{}",
location.path().display(),
location.selection().start().line().saturating_add(1),
location.selection().start().byte_column().saturating_add(1)
),
Self::External { display_uri } => write!(formatter, "unsupported: {display_uri}"),
}
}
}