Skip to content
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

RenameProvider #2788

Merged
merged 14 commits into from
Jul 17, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.github._1c_syntax.bsl.languageserver.jsonrpc.Diagnostics;
import com.github._1c_syntax.bsl.languageserver.jsonrpc.ProtocolExtension;
import com.github._1c_syntax.bsl.languageserver.providers.DocumentSymbolProvider;
import com.github._1c_syntax.bsl.languageserver.providers.RenameProvider;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.lsp4j.CallHierarchyRegistrationOptions;
Expand All @@ -44,6 +45,7 @@
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4j.InitializeResult;
import org.eclipse.lsp4j.ReferenceOptions;
import org.eclipse.lsp4j.RenameOptions;
import org.eclipse.lsp4j.SaveOptions;
import org.eclipse.lsp4j.SelectionRangeRegistrationOptions;
import org.eclipse.lsp4j.ServerCapabilities;
Expand Down Expand Up @@ -101,6 +103,7 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
capabilities.setCallHierarchyProvider(getCallHierarchyProvider());
capabilities.setSelectionRangeProvider(getSelectionRangeProvider());
capabilities.setColorProvider(getColorProvider());
capabilities.setRenameProvider(getRenameProvider());

var result = new InitializeResult(capabilities, serverInfo);

Expand Down Expand Up @@ -273,4 +276,11 @@ private static ColorProviderOptions getColorProvider() {
colorProviderOptions.setWorkDoneProgress(Boolean.FALSE);
return colorProviderOptions;
}

private static RenameOptions getRenameProvider() {
var renameOptions = new RenameOptions();
nixel2007 marked this conversation as resolved.
Show resolved Hide resolved
renameOptions.setWorkDoneProgress(Boolean.FALSE);
renameOptions.setPrepareProvider(Boolean.TRUE);
return renameOptions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.github._1c_syntax.bsl.languageserver.providers.FormatProvider;
import com.github._1c_syntax.bsl.languageserver.providers.HoverProvider;
import com.github._1c_syntax.bsl.languageserver.providers.ReferencesProvider;
import com.github._1c_syntax.bsl.languageserver.providers.RenameProvider;
import com.github._1c_syntax.bsl.languageserver.providers.SelectionRangeProvider;
import com.github._1c_syntax.bsl.languageserver.utils.Ranges;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -75,11 +76,16 @@
import org.eclipse.lsp4j.HoverParams;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.PrepareRenameParams;
import org.eclipse.lsp4j.PrepareRenameResult;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.ReferenceParams;
import org.eclipse.lsp4j.RenameParams;
import org.eclipse.lsp4j.SelectionRange;
import org.eclipse.lsp4j.SelectionRangeParams;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.services.TextDocumentService;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -108,6 +114,7 @@ public class BSLTextDocumentService implements TextDocumentService, ProtocolExte
private final CallHierarchyProvider callHierarchyProvider;
private final SelectionRangeProvider selectionRangeProvider;
private final ColorProvider colorProvider;
private final RenameProvider renameProvider;

@Override
public CompletableFuture<Hover> hover(HoverParams params) {
Expand Down Expand Up @@ -367,6 +374,29 @@ public CompletableFuture<Diagnostics> diagnostics(DiagnosticParams params) {
});
}
asosnoviy marked this conversation as resolved.
Show resolved Hide resolved

@Override
public CompletableFuture<Either<Range, PrepareRenameResult>> prepareRename(PrepareRenameParams params) {
var documentContext = context.getDocument(params.getTextDocument().getUri());
if (documentContext == null) {
return CompletableFuture.completedFuture(null);
}
asosnoviy marked this conversation as resolved.
Show resolved Hide resolved

var edits = renameProvider.getPrepareRename(params, documentContext);
return CompletableFuture.completedFuture(Either.forLeft(edits));
}

@Override
public CompletableFuture<WorkspaceEdit> rename(RenameParams params) {
var documentContext = context.getDocument(params.getTextDocument().getUri());
if (documentContext == null) {
return CompletableFuture.completedFuture(null);
}

asosnoviy marked this conversation as resolved.
Show resolved Hide resolved
var edits = renameProvider.getRename(params, documentContext);

return CompletableFuture.completedFuture(edits);
}

public void reset() {
context.clear();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright (c) 2018-2022
* 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.providers;

import com.github._1c_syntax.bsl.languageserver.context.DocumentContext;
import com.github._1c_syntax.bsl.languageserver.references.ReferenceIndex;
import com.github._1c_syntax.bsl.languageserver.references.ReferenceResolver;
import com.github._1c_syntax.bsl.languageserver.references.model.Reference;
import lombok.RequiredArgsConstructor;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.PrepareRenameParams;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.RenameParams;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
@RequiredArgsConstructor
public final class RenameProvider {

private final ReferenceResolver referenceResolver;
private final ReferenceIndex referenceIndex;

public WorkspaceEdit getRename(RenameParams params, DocumentContext documentContext) {

Position position = params.getPosition();

var reference = referenceResolver.findReference(
nixel2007 marked this conversation as resolved.
Show resolved Hide resolved
documentContext.getUri(), params.getPosition()
);

return new WorkspaceEdit(
Stream.concat(
nixel2007 marked this conversation as resolved.
Show resolved Hide resolved
referenceResolver.findReference(documentContext.getUri(), position)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тогда наверное тут можно взять её же, а не заново искать, разве нет?

.flatMap(Reference::getSourceDefinedSymbol)
.stream()
.map(referenceIndex::getReferencesTo)
.flatMap(Collection::stream),
reference.stream())
.collect(Collectors.groupingBy(
ref -> ref.getUri().toString(),
Collectors.mapping(Reference::getSelectionRange,
Collectors.mapping(range -> new TextEdit(range, params.getNewName()), Collectors.toList())))));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Немного переносов строк бы расставить тут и вокруг. Тяжело читается.

}

public Range getPrepareRename(
PrepareRenameParams params,
DocumentContext documentContext) {

return referenceResolver.findReference(
documentContext.getUri(), params.getPosition())
.map(Reference::getSelectionRange)
nixel2007 marked this conversation as resolved.
Show resolved Hide resolved
.orElse(null);
}
}