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 6c070eb
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 19 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 @@ -456,7 +456,7 @@ public void didOpen(DidOpenTextDocumentParams params) {
var file = openFilesCache.didOpen(uri, params.getTextDocument().getLanguageId(), params.getTextDocument().getText(), params.getTextDocument().getVersion());
CompletableFutures.computeAsync(cancelChecker -> {
String configScopeId;
moduleEventsProcessor.notifyBackendWithFileLanguageAndContent(file);
moduleEventsProcessor.notifyBackendWithFileLanguageAndContent(file, false);
var maybeWorkspaceFolder = workspaceFoldersManager.findFolderForFile(uri);
if (maybeWorkspaceFolder.isPresent()) {
configScopeId = maybeWorkspaceFolder.get().getUri().toString();
Expand All @@ -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.notifyBackendWithFileLanguageAndContent(file.get(), true);
return null;
});
}
Expand Down Expand Up @@ -568,7 +568,7 @@ public void didOpen(DidOpenNotebookDocumentParams params) {
openFilesCache.didClose(notebookUri);
}
CompletableFutures.computeAsync(cancelChecker -> {
moduleEventsProcessor.notifyBackendWithFileLanguageAndContent(versionedOpenFile);
moduleEventsProcessor.notifyBackendWithFileLanguageAndContent(versionedOpenFile, false);
var maybeWorkspaceFolder = workspaceFoldersManager.findFolderForFile(notebookUri);
if (maybeWorkspaceFolder.isPresent()) {
var configScopeId = maybeWorkspaceFolder.get().getUri().toString();
Expand All @@ -587,7 +587,7 @@ public void didChange(DidChangeNotebookDocumentParams params) {
} else {
var file = openNotebook.get().asVersionedOpenFile();
CompletableFutures.computeAsync(cancelChecker -> {
moduleEventsProcessor.notifyBackendWithFileLanguageAndContent(file);
moduleEventsProcessor.notifyBackendWithFileLanguageAndContent(file, true);
return null;
});
}
Expand Down Expand Up @@ -992,7 +992,7 @@ public CompletableFuture<Void> analyseOpenFileIgnoringExcludes(AnalyseOpenFileIg
if (versionedOpenFile != null) {
var workspaceFolder = workspaceFoldersManager.findFolderForFile(documentUri);
CompletableFutures.computeAsync(cancelChecker -> {
moduleEventsProcessor.notifyBackendWithFileLanguageAndContent(versionedOpenFile);
moduleEventsProcessor.notifyBackendWithFileLanguageAndContent(versionedOpenFile, false);
workspaceFolder.ifPresent(folder -> backendServiceFacade.getBackendService().analyzeFilesList(folder.getUri().toString(), List.of(documentUri)));
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 @@ -70,7 +70,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 +85,18 @@ 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) {
public void notifyBackendWithFileLanguageAndContent(VersionedOpenFile file, boolean didChangeContent) {
List<ClientFileDto> filesToNotify = new ArrayList<>();
var fileUri = file.getUri();
var fsPath = Paths.get(fileUri);
Expand All @@ -110,7 +115,14 @@ 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);
if (didChangeContent) {
// didChange
backendServiceFacade.getBackendService().updateFileSystem(List.of(), filesToNotify, List.of());
} else {
// We are simply enriching already added files with language and content information; The files were not actually modified
// i.e. didOpen
backendServiceFacade.getBackendService().updateFileSystem(filesToNotify, List.of(), List.of());
}
}

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 6c070eb

Please sign in to comment.