-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Выведение типов #3268
base: develop
Are you sure you want to change the base?
Выведение типов #3268
Changes from all commits
e6c0d03
cdafb42
243c8ff
3cb6bbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.github._1c_syntax.bsl.languageserver.providers; | ||
|
||
import com.github._1c_syntax.bsl.languageserver.context.DocumentContext; | ||
import com.github._1c_syntax.bsl.languageserver.context.symbol.SourceDefinedSymbol; | ||
import com.github._1c_syntax.bsl.languageserver.context.symbol.Symbol; | ||
import com.github._1c_syntax.bsl.languageserver.types.KnownTypes; | ||
import com.github._1c_syntax.bsl.languageserver.types.TypeResolver; | ||
import com.github._1c_syntax.bsl.languageserver.utils.Ranges; | ||
import com.github._1c_syntax.bsl.languageserver.utils.Trees; | ||
import com.github._1c_syntax.bsl.parser.BSLLexer; | ||
import lombok.RequiredArgsConstructor; | ||
import org.eclipse.lsp4j.CompletionItem; | ||
import org.eclipse.lsp4j.CompletionItemKind; | ||
import org.eclipse.lsp4j.CompletionList; | ||
import org.eclipse.lsp4j.CompletionParams; | ||
import org.eclipse.lsp4j.Range; | ||
import org.eclipse.lsp4j.SymbolKind; | ||
import org.eclipse.lsp4j.jsonrpc.messages.Either; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class CompletionProvider { | ||
|
||
private final TypeResolver typeResolver; | ||
private final KnownTypes knownTypes; | ||
|
||
public Either<List<CompletionItem>, CompletionList> getCompletions(DocumentContext documentContext, CompletionParams params) { | ||
|
||
var completionList = new CompletionList(); | ||
|
||
var position = params.getPosition(); | ||
var terminalNode = Trees.findTerminalNodeContainsPosition(documentContext.getAst(), position).orElseThrow(); | ||
|
||
if (terminalNode.getSymbol().getType() != BSLLexer.DOT) { | ||
return Either.forRight(completionList); | ||
} | ||
|
||
var previousToken = Trees.getPreviousTokenFromDefaultChannel(documentContext.getTokens(), terminalNode.getSymbol().getTokenIndex() - 1); | ||
var completionItems = previousToken | ||
.map(Ranges::create) | ||
.map(Range::getStart) | ||
.map(previousTokenPosition -> typeResolver.findTypes(documentContext.getUri(), previousTokenPosition)) | ||
.stream() | ||
.flatMap(Collection::stream) | ||
.map(knownTypes::getSymbolByType) | ||
.filter(Optional::isPresent) | ||
.map(Optional::get) | ||
.flatMap(symbol -> getChildren(symbol).stream()) | ||
.map(symbol -> { | ||
var completionItem = new CompletionItem(); | ||
completionItem.setLabel(symbol.getName()); | ||
completionItem.setKind(getCompletionItemKind(symbol.getSymbolKind())); | ||
|
||
return completionItem; | ||
}) | ||
.toList(); | ||
|
||
completionList.setItems(completionItems); | ||
|
||
return Either.forRight(completionList); | ||
} | ||
|
||
private CompletionItemKind getCompletionItemKind(SymbolKind symbolKind) { | ||
return switch (symbolKind) { | ||
case Class -> CompletionItemKind.Class; | ||
case Method -> CompletionItemKind.Method; | ||
case Variable -> CompletionItemKind.Variable; | ||
case Module -> CompletionItemKind.Module; | ||
default -> CompletionItemKind.Text; | ||
}; | ||
} | ||
|
||
private List<? extends Symbol> getChildren(Symbol symbol) { | ||
if (!(symbol instanceof SourceDefinedSymbol sourceDefinedSymbol)) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
return sourceDefinedSymbol.getChildren(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.github._1c_syntax.bsl.languageserver.types; | ||
|
||
import com.github._1c_syntax.bsl.languageserver.context.events.DocumentContextContentChangedEvent; | ||
import com.github._1c_syntax.bsl.languageserver.context.symbol.Symbol; | ||
import org.apache.commons.io.FilenameUtils; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
@Component | ||
public class KnownTypes { | ||
|
||
private final Map<Type, Symbol> knownTypes = new ConcurrentHashMap<>(); | ||
|
||
public void addType(Type type, Symbol symbol) { | ||
knownTypes.put(type, symbol); | ||
} | ||
|
||
public Optional<Symbol> getSymbolByType(Type type) { | ||
return Optional.ofNullable(knownTypes.get(type)); | ||
} | ||
|
||
public void clear() { | ||
knownTypes.clear(); | ||
} | ||
|
||
@EventListener | ||
public void handleEvent(DocumentContextContentChangedEvent event) { | ||
var documentContext = event.getSource(); | ||
// TODO: this logic should be moved to somewhere else. It will break for BSL files. | ||
var typeName = FilenameUtils.getBaseName(documentContext.getUri().getPath()); | ||
var module = documentContext.getSymbolTree().getModule(); | ||
|
||
addType(new Type(typeName), module); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* This file is a part of BSL Language Server. | ||
* | ||
* Copyright (c) 2018-2024 | ||
* Alexey Sosnoviy <[email protected]>, Nikita Fedkin <[email protected]> and contributors | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
* | ||
* BSL Language Server is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3.0 of the License, or (at your option) any later version. | ||
* | ||
* BSL Language Server is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with BSL Language Server. | ||
*/ | ||
package com.github._1c_syntax.bsl.languageserver.types; | ||
|
||
import lombok.Value; | ||
|
||
@Value | ||
public class Type { | ||
private final String name; | ||
Check warning on line 28 in src/main/java/com/github/_1c_syntax/bsl/languageserver/types/Type.java GitHub Actions / Qodana for JVM@Value modifiers
Check warning on line 28 in src/main/java/com/github/_1c_syntax/bsl/languageserver/types/Type.java GitHub Actions / Qodana for JVM@Value modifiers
|
||
Check warning Code scanning / QDJVM @Value modifiers Warning
@Value already marks non-static, package-local fields private.
|
||
} |
Check warning
Code scanning / QDJVM
@Value modifiers Warning