-
Notifications
You must be signed in to change notification settings - Fork 116
Выведение типов #3268
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
Open
nixel2007
wants to merge
4
commits into
develop
Choose a base branch
from
feature/typeResolver
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Выведение типов #3268
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e6c0d03
Драфт внешнего вычислителя типов
nixel2007 cdafb42
Расчет типа возвращаемого значения локального метода и описания перем…
nixel2007 243c8ff
Работа с ховером переведена на reference-api. Исправлено возможное NP…
nixel2007 3cb6bbf
draft of completion provider
nixel2007 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/main/java/com/github/_1c_syntax/bsl/languageserver/providers/CompletionProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/com/github/_1c_syntax/bsl/languageserver/types/KnownTypes.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/github/_1c_syntax/bsl/languageserver/types/Type.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 warningCode scanning / QDJVMC @Value modifiers
@Value already marks non-static, package-local fields private.
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / QDJVMC
@Value modifiers