1use language::LanguageId;
2use project::ProjectEntryId;
3use std::borrow::Cow;
4use std::ops::Range;
5use std::sync::Arc;
6use text::{Bias, BufferId, Rope};
7
8use crate::outline::OutlineDeclaration;
9
10#[derive(Debug, Clone, Eq, PartialEq, Hash)]
11pub struct Identifier {
12 pub name: Arc<str>,
13 pub language_id: LanguageId,
14}
15
16slotmap::new_key_type! {
17 pub struct DeclarationId;
18}
19
20#[derive(Debug, Clone)]
21pub enum Declaration {
22 File {
23 project_entry_id: ProjectEntryId,
24 declaration: FileDeclaration,
25 },
26 Buffer {
27 project_entry_id: ProjectEntryId,
28 buffer_id: BufferId,
29 rope: Rope,
30 declaration: BufferDeclaration,
31 },
32}
33
34const ITEM_TEXT_TRUNCATION_LENGTH: usize = 1024;
35
36impl Declaration {
37 pub fn identifier(&self) -> &Identifier {
38 match self {
39 Declaration::File { declaration, .. } => &declaration.identifier,
40 Declaration::Buffer { declaration, .. } => &declaration.identifier,
41 }
42 }
43
44 pub fn parent(&self) -> Option<DeclarationId> {
45 match self {
46 Declaration::File { declaration, .. } => declaration.parent,
47 Declaration::Buffer { declaration, .. } => declaration.parent,
48 }
49 }
50
51 pub fn as_buffer(&self) -> Option<&BufferDeclaration> {
52 match self {
53 Declaration::File { .. } => None,
54 Declaration::Buffer { declaration, .. } => Some(declaration),
55 }
56 }
57
58 pub fn project_entry_id(&self) -> ProjectEntryId {
59 match self {
60 Declaration::File {
61 project_entry_id, ..
62 } => *project_entry_id,
63 Declaration::Buffer {
64 project_entry_id, ..
65 } => *project_entry_id,
66 }
67 }
68
69 pub fn item_range(&self) -> Range<usize> {
70 match self {
71 Declaration::File { declaration, .. } => declaration.item_range_in_file.clone(),
72 Declaration::Buffer { declaration, .. } => declaration.item_range.clone(),
73 }
74 }
75
76 pub fn item_text(&self) -> (Cow<'_, str>, bool) {
77 match self {
78 Declaration::File { declaration, .. } => (
79 declaration.text.as_ref().into(),
80 declaration.text_is_truncated,
81 ),
82 Declaration::Buffer {
83 rope, declaration, ..
84 } => (
85 rope.chunks_in_range(declaration.item_range.clone())
86 .collect::<Cow<str>>(),
87 declaration.item_range_is_truncated,
88 ),
89 }
90 }
91
92 pub fn signature_text(&self) -> (Cow<'_, str>, bool) {
93 match self {
94 Declaration::File { declaration, .. } => (
95 declaration.text[declaration.signature_range_in_text.clone()].into(),
96 declaration.signature_is_truncated,
97 ),
98 Declaration::Buffer {
99 rope, declaration, ..
100 } => (
101 rope.chunks_in_range(declaration.signature_range.clone())
102 .collect::<Cow<str>>(),
103 declaration.signature_range_is_truncated,
104 ),
105 }
106 }
107
108 pub fn signature_range_in_item_text(&self) -> Range<usize> {
109 match self {
110 Declaration::File { declaration, .. } => declaration.signature_range_in_text.clone(),
111 Declaration::Buffer { declaration, .. } => {
112 declaration.signature_range.start - declaration.item_range.start
113 ..declaration.signature_range.end - declaration.item_range.start
114 }
115 }
116 }
117}
118
119fn expand_range_to_line_boundaries_and_truncate(
120 range: &Range<usize>,
121 limit: usize,
122 rope: &Rope,
123) -> (Range<usize>, bool) {
124 let mut point_range = rope.offset_to_point(range.start)..rope.offset_to_point(range.end);
125 point_range.start.column = 0;
126 point_range.end.row += 1;
127 point_range.end.column = 0;
128
129 let mut item_range =
130 rope.point_to_offset(point_range.start)..rope.point_to_offset(point_range.end);
131 let is_truncated = item_range.len() > limit;
132 if is_truncated {
133 item_range.end = item_range.start + limit;
134 }
135 item_range.end = rope.clip_offset(item_range.end, Bias::Left);
136 (item_range, is_truncated)
137}
138
139#[derive(Debug, Clone)]
140pub struct FileDeclaration {
141 pub parent: Option<DeclarationId>,
142 pub identifier: Identifier,
143 /// offset range of the declaration in the file, expanded to line boundaries and truncated
144 pub item_range_in_file: Range<usize>,
145 /// text of `item_range_in_file`
146 pub text: Arc<str>,
147 /// whether `text` was truncated
148 pub text_is_truncated: bool,
149 /// offset range of the signature within `text`
150 pub signature_range_in_text: Range<usize>,
151 /// whether `signature` was truncated
152 pub signature_is_truncated: bool,
153}
154
155impl FileDeclaration {
156 pub fn from_outline(declaration: OutlineDeclaration, rope: &Rope) -> FileDeclaration {
157 let (item_range_in_file, text_is_truncated) = expand_range_to_line_boundaries_and_truncate(
158 &declaration.item_range,
159 ITEM_TEXT_TRUNCATION_LENGTH,
160 rope,
161 );
162
163 // TODO: consider logging if unexpected
164 let signature_start = declaration
165 .signature_range
166 .start
167 .saturating_sub(item_range_in_file.start);
168 let mut signature_end = declaration
169 .signature_range
170 .end
171 .saturating_sub(item_range_in_file.start);
172 let signature_is_truncated = signature_end > item_range_in_file.len();
173 if signature_is_truncated {
174 signature_end = item_range_in_file.len();
175 }
176
177 FileDeclaration {
178 parent: None,
179 identifier: declaration.identifier,
180 signature_range_in_text: signature_start..signature_end,
181 signature_is_truncated,
182 text: rope
183 .chunks_in_range(item_range_in_file.clone())
184 .collect::<String>()
185 .into(),
186 text_is_truncated,
187 item_range_in_file,
188 }
189 }
190}
191
192#[derive(Debug, Clone)]
193pub struct BufferDeclaration {
194 pub parent: Option<DeclarationId>,
195 pub identifier: Identifier,
196 pub item_range: Range<usize>,
197 pub item_range_is_truncated: bool,
198 pub signature_range: Range<usize>,
199 pub signature_range_is_truncated: bool,
200}
201
202impl BufferDeclaration {
203 pub fn from_outline(declaration: OutlineDeclaration, rope: &Rope) -> Self {
204 let (item_range, item_range_is_truncated) = expand_range_to_line_boundaries_and_truncate(
205 &declaration.item_range,
206 ITEM_TEXT_TRUNCATION_LENGTH,
207 rope,
208 );
209 let (signature_range, signature_range_is_truncated) =
210 expand_range_to_line_boundaries_and_truncate(
211 &declaration.signature_range,
212 ITEM_TEXT_TRUNCATION_LENGTH,
213 rope,
214 );
215 Self {
216 parent: None,
217 identifier: declaration.identifier,
218 item_range,
219 item_range_is_truncated,
220 signature_range,
221 signature_range_is_truncated,
222 }
223 }
224}