|
| 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 | +} |
0 commit comments