Skip to content

Commit

Permalink
SLLS-281 distinguish added and modified files
Browse files Browse the repository at this point in the history
  • Loading branch information
sophio-japharidze-sonarsource committed Nov 28, 2024
1 parent 51d86ce commit 3da0767
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<properties>
<jdk.min.version>17</jdk.min.version>
<sonarlint.core.version>10.11.0.79608</sonarlint.core.version>
<sonarlint.core.version>10.11.0.79653</sonarlint.core.version>
<!-- Version used by Xodus -->
<kotlin.version>1.6.10</kotlin.version>
<!-- analyzers used for tests -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ public void didChange(DidChangeTextDocumentParams params) {
} else {
// VSCode sends us full file content in the change event
CompletableFutures.computeAsync(cancelChecker -> {
moduleEventsProcessor.notifyBackendWithFileLanguageAndContent(file.get());
moduleEventsProcessor.notifyBackendWithUpdatedContent(file.get());
return null;
});
}
Expand Down Expand Up @@ -587,7 +587,7 @@ public void didChange(DidChangeNotebookDocumentParams params) {
} else {
var file = openNotebook.get().asVersionedOpenFile();
CompletableFutures.computeAsync(cancelChecker -> {
moduleEventsProcessor.notifyBackendWithFileLanguageAndContent(file);
moduleEventsProcessor.notifyBackendWithUpdatedContent(file);
return null;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ public CompletableFuture<GetAllProjectsResponse> getAllProjects(Either<Transient
return initializedBackend().getConnectionService().getAllProjects(new GetAllProjectsParams(transientConnection));
}

public void updateFileSystem(List<URI> deletedFileUris, List<ClientFileDto> addedOrChangedFiles) {
initializedBackend().getFileService().didUpdateFileSystem(new DidUpdateFileSystemParams(deletedFileUris, addedOrChangedFiles));
public void updateFileSystem(List<ClientFileDto> addedFiles, List<ClientFileDto> changedFiles, List<URI> deletedFileUris) {
initializedBackend().getFileService().didUpdateFileSystem(new DidUpdateFileSystemParams(addedFiles, changedFiles, deletedFileUris));
}

public CompletableFuture<ListAllResponse> getAllTaints(String folderUri) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.concurrent.Executors;
import org.eclipse.lsp4j.FileChangeType;
import org.eclipse.lsp4j.FileEvent;
import org.jetbrains.annotations.NotNull;
import org.sonarsource.sonarlint.core.commons.api.SonarLanguage;
import org.sonarsource.sonarlint.core.rpc.protocol.common.ClientFileDto;
import org.sonarsource.sonarlint.core.rpc.protocol.common.Language;
Expand Down Expand Up @@ -70,7 +71,8 @@ public void didChangeWatchedFiles(List<FileEvent> changes) {

private void notifyBackend(List<FileEvent> changes) {
List<URI> deletedFileUris = new ArrayList<>();
List<ClientFileDto> addedOrChangedFiles = new ArrayList<>();
List<ClientFileDto> addedFiles = new ArrayList<>();
List<ClientFileDto> changedFiles = new ArrayList<>();
changes.forEach(event -> {
var fileUri = URI.create(event.getUri());
if (event.getType() == FileChangeType.Deleted) {
Expand All @@ -84,14 +86,30 @@ private void notifyBackend(List<FileEvent> changes) {
var relativePath = baseDir.relativize(fsPath);
var folderUri = folder.getUri().toString();
var isTest = isTestFile(fileUri, settings);
addedOrChangedFiles.add(new ClientFileDto(fileUri, relativePath, folderUri, isTest, StandardCharsets.UTF_8.name(), fsPath, null, null, true));
if (event.getType() == FileChangeType.Created) {
addedFiles.add(new ClientFileDto(fileUri, relativePath, folderUri, isTest, StandardCharsets.UTF_8.name(), fsPath, null, null, true));
} else {
changedFiles.add(new ClientFileDto(fileUri, relativePath, folderUri, isTest, StandardCharsets.UTF_8.name(), fsPath, null, null, true));
}
});
}
});
backendServiceFacade.getBackendService().updateFileSystem(deletedFileUris, addedOrChangedFiles);
backendServiceFacade.getBackendService().updateFileSystem(addedFiles, changedFiles, deletedFileUris);
}

public void notifyBackendWithFileLanguageAndContent(VersionedOpenFile file) {
List<ClientFileDto> openedFiles = getClientFileDtos(file);
// We are simply enriching already added files with language and content information; The files were not actually modified
// i.e. didOpen
backendServiceFacade.getBackendService().updateFileSystem(openedFiles, List.of(), List.of());
}

public void notifyBackendWithUpdatedContent(VersionedOpenFile file) {
List<ClientFileDto> changedFiles = getClientFileDtos(file);
backendServiceFacade.getBackendService().updateFileSystem(List.of(), changedFiles, List.of());
}

private @NotNull List<ClientFileDto> getClientFileDtos(VersionedOpenFile file) {
List<ClientFileDto> filesToNotify = new ArrayList<>();
var fileUri = file.getUri();
var fsPath = Paths.get(fileUri);
Expand All @@ -110,7 +128,7 @@ public void notifyBackendWithFileLanguageAndContent(VersionedOpenFile file) {
filesToNotify.add(new ClientFileDto(fileUri, fsPath, ROOT_CONFIGURATION_SCOPE, isTest, StandardCharsets.UTF_8.name(),
fsPath, file.getContent(), sqLanguage != null ? Language.valueOf(sqLanguage.name()) : null, true));
});
backendServiceFacade.getBackendService().updateFileSystem(List.of(), filesToNotify);
return filesToNotify;
}

private boolean isTestFile(URI fileUri, WorkspaceFolderSettings settings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ void shouldForwardOpenIssueRequest() {
var fileUri = fileInAWorkspaceFolderPath.toUri();
var textRangeDto = new TextRangeDto(1, 2, 3, 4);
var issueDetailsDto = new IssueDetailsDto(textRangeDto, "rule:S1234",
"issueKey", FILE_PYTHON, "branch", "PR", "this is wrong",
"issueKey", FILE_PYTHON, "this is wrong",
"29.09.2023", "print('ddd')", false, List.of());
var showIssueParams = new ShowIssueParams(fileUri.toString(), issueDetailsDto);

Expand All @@ -625,7 +625,7 @@ void shouldForwardOpenIssueRequestWithoutRuleDescriptionWhenBindingDoesNotExist(
var fileUri = fileInAWorkspaceFolderPath.toUri();
var textRangeDto = new TextRangeDto(1, 2, 3, 4);
var issueDetailsDto = new IssueDetailsDto(textRangeDto, "rule:S1234",
"issueKey", FILE_PYTHON, "bb", null, "this is wrong", "29.09.2023", "print('ddd')",
"issueKey", FILE_PYTHON, "this is wrong", "29.09.2023", "print('ddd')",
false, List.of());
when(bindingManager.getBindingIfExists(fileUri))
.thenReturn(Optional.empty());
Expand All @@ -643,7 +643,7 @@ void shouldForwardOpenIssueRequestWithRuleDescriptionWhenBindingDoesExist() {
var fileUri = fileInAWorkspaceFolderPath.toUri();
var textRangeDto = new TextRangeDto(1, 2, 3, 4);
var issueDetailsDto = new IssueDetailsDto(textRangeDto, "rule:S1234",
"issueKey", FILE_PYTHON, "bb", null, "this is wrong", "29.09.2023", "print('ddd')",
"issueKey", FILE_PYTHON, "this is wrong", "29.09.2023", "print('ddd')",
false, List.of());
when(bindingManager.getBindingIfExists(fileUri))
.thenReturn(Optional.of(new ProjectBinding("connectionId", "projectKey")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void shouldBuildCommandParamsFromShowIssueParams() {

var textRangeDto = new TextRangeDto(1, 0, 1, 13);
var showIssueParams = new ShowIssueParams(workspaceFolderPath.toUri().toString(), new IssueDetailsDto(textRangeDto, "rule:S1234",
"issueKey", Path.of("myFile.py"), "branch", "pr", "this is wrong",
"issueKey", Path.of("myFile.py"), "this is wrong",
"29.09.2023", "print('1234')", false, flows));

var result = new ShowAllLocationsCommand.Param(showIssueParams, "connectionId", true);
Expand All @@ -152,7 +152,7 @@ void shouldBuildCommandParamsFromShowIssueParams() {
void shouldBuildCommandParamsFromShowIssueParamsForFileLevelIssue() {
var textRangeDto = new TextRangeDto(0, 0, 0, 0);
var showIssueParams = new ShowIssueParams(workspaceFolderPath.toUri().toString(), new IssueDetailsDto(textRangeDto, "rule:S1234",
"issueKey", Path.of("myFile.py"), "branch", null, "this is wrong",
"issueKey", Path.of("myFile.py"), "this is wrong",
"29.09.2023", """
print('1234')
print('aa')
Expand All @@ -168,7 +168,7 @@ void shouldBuildCommandParamsFromShowIssueParamsForFileLevelIssue() {
void shouldBuildCommandParamsFromShowIssueParamsForInvalidTextRange() {
var textRangeDto = new TextRangeDto(-1, 0, -2, 0);
var showIssueParams = new ShowIssueParams(workspaceFolderPath.toUri().toString(), new IssueDetailsDto(textRangeDto, "rule:S1234",
"issueKey", Path.of("myFile.py"), "bb", "1234", "this is wrong",
"issueKey", Path.of("myFile.py"), "this is wrong",
"29.09.2023", "print('1234')", false, List.of()));

var result = new ShowAllLocationsCommand.Param(showIssueParams, "connectionId", true);
Expand Down

0 comments on commit 3da0767

Please sign in to comment.