Skip to content

Commit e7e8f38

Browse files
committed
Вынос общей логики символов переменных в абстрактный класс
1 parent 65ff00e commit e7e8f38

File tree

6 files changed

+298
-323
lines changed

6 files changed

+298
-323
lines changed

src/jmh/java/com/github/_1c_syntax/bsl/languageserver/context/symbol/VariableSymbolCreate.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,17 @@ public void setup() {
5454
@Fork(value = 2, warmups = 2)
5555
@Warmup(time = 5, iterations = 3)
5656
public void createVariableSymbols(Blackhole bh) {
57-
var test = getVariableSymbolBuilder().build();
58-
59-
bh.consume(test);
60-
}
61-
62-
private VariableSymbolBuilder getVariableSymbolBuilder() {
63-
return VariableSymbol.builder()
57+
var test = VariableSymbol.builder()
6458
.name("test")
6559
.owner(null)
6660
.range(range)
6761
.variableNameRange(range)
6862
.export(true)
6963
.kind(VariableKind.MODULE)
7064
.description(Optional.empty())
71-
.scope(null);
65+
.scope(null).build();
66+
67+
bh.consume(test);
7268
}
7369

7470
}
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
/*
2+
* This file is a part of BSL Language Server.
3+
*
4+
* Copyright (c) 2018-2022
5+
* Alexey Sosnoviy <labotamy@gmail.com>, Nikita Fedkin <nixel2007@gmail.com> and contributors
6+
*
7+
* SPDX-License-Identifier: LGPL-3.0-or-later
8+
*
9+
* BSL Language Server is free software; you can redistribute it and/or
10+
* modify it under the terms of the GNU Lesser General Public
11+
* License as published by the Free Software Foundation; either
12+
* version 3.0 of the License, or (at your option) any later version.
13+
*
14+
* BSL Language Server is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
* Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public
20+
* License along with BSL Language Server.
21+
*/
22+
package com.github._1c_syntax.bsl.languageserver.context.symbol;
23+
24+
import com.github._1c_syntax.bsl.languageserver.context.DocumentContext;
25+
import com.github._1c_syntax.bsl.languageserver.context.symbol.variable.VariableDescription;
26+
import com.github._1c_syntax.bsl.languageserver.context.symbol.variable.VariableKind;
27+
import lombok.Builder;
28+
import lombok.EqualsAndHashCode;
29+
import lombok.Getter;
30+
import lombok.Setter;
31+
import lombok.ToString;
32+
import lombok.Value;
33+
import lombok.experimental.Accessors;
34+
import lombok.experimental.NonFinal;
35+
import org.eclipse.lsp4j.Range;
36+
import org.eclipse.lsp4j.SymbolKind;
37+
38+
import java.util.Collections;
39+
import java.util.List;
40+
import java.util.Optional;
41+
42+
/**
43+
* Общая реализация символа переменной.
44+
*/
45+
@Value
46+
@NonFinal
47+
@Builder(builderClassName = "Builder")
48+
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
49+
@ToString(exclude = {"children", "parent"})
50+
public abstract class AbstractVariableSymbol implements VariableSymbol {
51+
52+
/**
53+
* Имя переменной.
54+
*/
55+
@EqualsAndHashCode.Include
56+
String name;
57+
58+
/**
59+
* Область доступности символа. Метод или модуль.
60+
*/
61+
SourceDefinedSymbol scope;
62+
63+
/**
64+
* Файл в котором располагается переменная.
65+
*/
66+
@EqualsAndHashCode.Include
67+
DocumentContext owner;
68+
69+
/**
70+
* Символ, внутри которого располагается данный символ.
71+
*/
72+
@Getter
73+
@Setter
74+
@NonFinal
75+
Optional<SourceDefinedSymbol> parent;
76+
77+
/**
78+
* Список "детей" символа - символов, которые располагаются внутри данного символа.
79+
*/
80+
@Getter
81+
List<SourceDefinedSymbol> children;
82+
83+
/**
84+
* Тип переменной.
85+
*/
86+
byte kind;
87+
88+
/**
89+
* Признак экспортной переменной.
90+
*/
91+
boolean export;
92+
93+
/**
94+
* Описание переменной.
95+
*/
96+
Optional<VariableDescription> description;
97+
98+
@Override
99+
public SymbolKind getSymbolKind() {
100+
return SymbolKind.Variable;
101+
}
102+
103+
@Override
104+
public VariableKind getKind() {
105+
return VariableKind.values()[kind];
106+
}
107+
108+
@Override
109+
@EqualsAndHashCode.Include
110+
public abstract Range getVariableNameRange();
111+
112+
@Override
113+
public void accept(SymbolTreeVisitor visitor) {
114+
visitor.visitVariable(this);
115+
}
116+
117+
@Override
118+
public Range getSelectionRange() {
119+
return getVariableNameRange();
120+
}
121+
122+
public static class Builder {
123+
124+
@Setter
125+
@Accessors(fluent = true, chain = true)
126+
private VariableKind kind;
127+
128+
@Setter
129+
@Accessors(fluent = true, chain = true)
130+
Optional<SourceDefinedSymbol> parent = Optional.empty();
131+
132+
@Setter
133+
@Accessors(fluent = true, chain = true)
134+
List<SourceDefinedSymbol> children = Collections.emptyList();
135+
136+
private int startLine;
137+
private int startCharacter;
138+
private int endLine;
139+
private int endCharacter;
140+
private int variableNameLine;
141+
private int variableNameStartCharacter;
142+
private int variableNameEndCharacter;
143+
144+
public Builder range(Range range) {
145+
var start = range.getStart();
146+
var end = range.getEnd();
147+
startLine = start.getLine();
148+
startCharacter = start.getCharacter();
149+
endLine = end.getLine();
150+
endCharacter = end.getCharacter();
151+
152+
return this;
153+
}
154+
155+
public Builder variableNameRange(Range range) {
156+
var start = range.getStart();
157+
var end = range.getEnd();
158+
variableNameLine = start.getLine();
159+
variableNameStartCharacter = start.getCharacter();
160+
variableNameEndCharacter = end.getCharacter();
161+
162+
return this;
163+
}
164+
165+
public VariableSymbol build() {
166+
167+
// Ленивое булево вычисление диапазона переменной
168+
var shortBased = startLine <= Short.MAX_VALUE
169+
&& endLine <= Short.MAX_VALUE
170+
&& startCharacter <= Short.MAX_VALUE
171+
&& endCharacter <= Short.MAX_VALUE
172+
&& variableNameLine <= Short.MAX_VALUE
173+
&& variableNameStartCharacter <= Short.MAX_VALUE
174+
&& variableNameEndCharacter <= Short.MAX_VALUE;
175+
176+
if (shortBased) {
177+
return new ShortBasedVariableSymbol(
178+
name,
179+
scope,
180+
owner,
181+
parent,
182+
children,
183+
(byte) kind.ordinal(),
184+
export,
185+
description,
186+
(short) startLine,
187+
(short) startCharacter,
188+
(short) endLine,
189+
(short) endCharacter,
190+
(short) variableNameLine,
191+
(short) variableNameStartCharacter,
192+
(short) variableNameEndCharacter
193+
);
194+
} else {
195+
return new IntBasedVariableSymbol(
196+
name,
197+
scope,
198+
owner,
199+
parent,
200+
children,
201+
(byte) kind.ordinal(),
202+
export,
203+
description,
204+
startLine,
205+
startCharacter,
206+
endLine,
207+
endCharacter,
208+
variableNameLine,
209+
variableNameStartCharacter,
210+
variableNameEndCharacter
211+
);
212+
}
213+
}
214+
}
215+
216+
}

src/main/java/com/github/_1c_syntax/bsl/languageserver/context/symbol/IntBasedVariableSymbol.java

Lines changed: 32 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,26 @@
2323

2424
import com.github._1c_syntax.bsl.languageserver.context.DocumentContext;
2525
import com.github._1c_syntax.bsl.languageserver.context.symbol.variable.VariableDescription;
26-
import com.github._1c_syntax.bsl.languageserver.context.symbol.variable.VariableKind;
2726
import com.github._1c_syntax.bsl.languageserver.utils.Ranges;
2827
import lombok.AccessLevel;
29-
import lombok.Builder;
3028
import lombok.EqualsAndHashCode;
3129
import lombok.Getter;
32-
import lombok.Setter;
3330
import lombok.ToString;
3431
import lombok.Value;
3532
import lombok.experimental.NonFinal;
3633
import org.eclipse.lsp4j.Range;
37-
import org.eclipse.lsp4j.SymbolKind;
3834

39-
import java.util.Collections;
4035
import java.util.List;
4136
import java.util.Optional;
4237

38+
/**
39+
* Реализация символа переменной, хранящая позицию в виде int.
40+
*/
4341
@Value
4442
@NonFinal
45-
@Builder
46-
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
47-
@ToString(exclude = {"children", "parent"})
48-
public class IntBasedVariableSymbol implements VariableSymbol {
49-
50-
/**
51-
* Имя переменной.
52-
*/
53-
@EqualsAndHashCode.Include
54-
String name;
55-
56-
/**
57-
* Область доступности символа. Метод или модуль.
58-
*/
59-
SourceDefinedSymbol scope;
60-
61-
/**
62-
* Файл в котором располагается переменная.
63-
*/
64-
@EqualsAndHashCode.Include
65-
DocumentContext owner;
43+
@ToString(callSuper = true)
44+
@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = true)
45+
public class IntBasedVariableSymbol extends AbstractVariableSymbol {
6646

6747
@Getter(AccessLevel.NONE)
6848
int startLine;
@@ -80,37 +60,32 @@ public class IntBasedVariableSymbol implements VariableSymbol {
8060
@Getter(AccessLevel.NONE)
8161
int variableNameEndCharacter;
8262

83-
@Getter
84-
@Setter
85-
@Builder.Default
86-
@NonFinal
87-
Optional<SourceDefinedSymbol> parent = Optional.empty();
88-
89-
@Builder.Default
90-
List<SourceDefinedSymbol> children = Collections.emptyList();
91-
92-
/**
93-
* Тип переменной.
94-
*/
95-
byte kind;
96-
97-
/**
98-
* Признак экспортной переменной.
99-
*/
100-
boolean export;
101-
102-
/**
103-
* Описание переменной.
104-
*/
105-
Optional<VariableDescription> description;
106-
107-
public SymbolKind getSymbolKind() {
108-
return SymbolKind.Variable;
109-
}
110-
111-
@Override
112-
public VariableKind getKind() {
113-
return VariableKind.values()[kind];
63+
public IntBasedVariableSymbol(
64+
String name,
65+
SourceDefinedSymbol scope,
66+
DocumentContext owner,
67+
Optional<SourceDefinedSymbol> parent,
68+
List<SourceDefinedSymbol> children,
69+
byte kind,
70+
boolean export,
71+
Optional<VariableDescription> description,
72+
int startLine,
73+
int startCharacter,
74+
int endLine,
75+
int endCharacter,
76+
int variableNameLine,
77+
int variableNameStartCharacter,
78+
int variableNameEndCharacter
79+
) {
80+
super(name, scope, owner, parent, children, kind, export, description);
81+
82+
this.startLine = startLine;
83+
this.startCharacter = startCharacter;
84+
this.endLine = endLine;
85+
this.endCharacter = endCharacter;
86+
this.variableNameLine = variableNameLine;
87+
this.variableNameStartCharacter = variableNameStartCharacter;
88+
this.variableNameEndCharacter = variableNameEndCharacter;
11489
}
11590

11691
@Override
@@ -129,14 +104,4 @@ public Range getVariableNameRange() {
129104
);
130105
}
131106

132-
@Override
133-
public void accept(SymbolTreeVisitor visitor) {
134-
visitor.visitVariable(this);
135-
}
136-
137-
@Override
138-
public Range getSelectionRange() {
139-
return getVariableNameRange();
140-
}
141-
142107
}

0 commit comments

Comments
 (0)