emmylua_code_analysis/db_index/declaration/
scope.rs1use rowan::{TextRange, TextSize};
2
3use crate::FileId;
4
5use super::LuaDeclId;
6
7#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)]
8pub enum LuaScopeKind {
9 Normal,
10 Repeat,
11 LocalOrAssignStat,
12 ForRange,
13 FuncStat,
14 MethodStat,
16}
17
18#[derive(Debug, Eq, PartialEq, Hash, Clone)]
19pub struct LuaScope {
20 parent: Option<LuaScopeId>,
21 children: Vec<ScopeOrDeclId>,
22 range: TextRange,
23 kind: LuaScopeKind,
24 id: LuaScopeId,
25}
26
27impl LuaScope {
28 pub fn new(range: TextRange, kind: LuaScopeKind, id: LuaScopeId) -> Self {
29 Self {
30 parent: None,
31 children: Vec::new(),
32 range,
33 kind,
34 id,
35 }
36 }
37
38 pub fn add_decl(&mut self, decl: LuaDeclId) {
39 self.children.push(ScopeOrDeclId::Decl(decl));
40 }
41
42 pub fn add_child(&mut self, child: LuaScopeId) {
43 self.children.push(ScopeOrDeclId::Scope(child));
44 }
45
46 pub fn get_parent(&self) -> Option<LuaScopeId> {
47 self.parent
48 }
49
50 pub(crate) fn set_parent(&mut self, parent: Option<LuaScopeId>) {
51 self.parent = parent;
52 }
53
54 pub fn get_children(&self) -> &[ScopeOrDeclId] {
55 &self.children
56 }
57
58 pub fn get_range(&self) -> TextRange {
59 self.range
60 }
61
62 pub fn get_kind(&self) -> LuaScopeKind {
63 self.kind
64 }
65
66 pub fn get_position(&self) -> TextSize {
67 self.range.start()
68 }
69
70 pub fn get_id(&self) -> LuaScopeId {
71 self.id
72 }
73}
74
75#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)]
76pub struct LuaScopeId {
77 pub file_id: FileId,
78 pub id: u32,
79}
80
81#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)]
82pub enum ScopeOrDeclId {
83 Scope(LuaScopeId),
84 Decl(LuaDeclId),
85}
86
87impl From<LuaDeclId> for ScopeOrDeclId {
88 fn from(decl_id: LuaDeclId) -> Self {
89 Self::Decl(decl_id)
90 }
91}
92
93impl From<LuaScopeId> for ScopeOrDeclId {
94 fn from(scope_id: LuaScopeId) -> Self {
95 Self::Scope(scope_id)
96 }
97}
98
99impl From<&LuaDeclId> for ScopeOrDeclId {
100 fn from(decl_id: &LuaDeclId) -> Self {
101 Self::Decl(*decl_id)
102 }
103}
104
105impl From<&LuaScopeId> for ScopeOrDeclId {
106 fn from(scope_id: &LuaScopeId) -> Self {
107 Self::Scope(*scope_id)
108 }
109}