Skip to content

Commit

Permalink
SLVSCODE-666 show notification when file is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
sophio-japharidze-sonarsource committed Oct 14, 2024
1 parent 9bfc7d8 commit c383ada
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
8 changes: 7 additions & 1 deletion src/fixSuggestions/fixSuggestionsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ShowFixSuggestionParams } from "../lsp/protocol";
import * as vscode from "vscode";
import { logToSonarLintOutput } from "../util/logging";
import { SonarLintExtendedLanguageClient } from "../lsp/client";
import { pathExists } from "../util/uri";
import { showNoFileWithUriError } from "../util/showMessage";

export class FixSuggestionService {
private static readonly END_OF_LINE_OFFSET = 10000;
Expand All @@ -19,6 +21,10 @@ export class FixSuggestionService {
showFixSuggestion = async (params : ShowFixSuggestionParams) => {
try {
const fileUri = vscode.Uri.parse(params.fileUri);
if (!(await pathExists(fileUri))) {
showNoFileWithUriError(fileUri);
return;
}
const editor = await vscode.window.showTextDocument(fileUri);
const wsedit = new vscode.WorkspaceEdit();
for (const edit of params.textEdits) {
Expand All @@ -37,7 +43,7 @@ export class FixSuggestionService {
// result will be false if no edits were applied
this.client.fixSuggestionResolved(params.suggestionId, result);
} catch (error) {
logToSonarLintOutput('Failed to apply edit:'.concat(error.message));
logToSonarLintOutput('Failed to apply edit: '.concat(error.message));
}
}

Expand Down
21 changes: 5 additions & 16 deletions src/issue/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

import { SonarLintExtendedLanguageClient } from '../lsp/client';
import * as VSCode from 'vscode';
import { code2ProtocolConverter, getFileNameFromFullPath, getRelativePathWithFileNameFromFullPath, protocol2CodeConverter } from '../util/uri';
import { showNoActiveFileOpenWarning } from '../util/showMessage';
import { code2ProtocolConverter, getFileNameFromFullPath, getRelativePathWithFileNameFromFullPath, protocol2CodeConverter, pathExists } from '../util/uri';
import { showNoActiveFileOpenWarning, showNoFileWithUriError } from '../util/showMessage';
import { AnalysisFile, CheckIssueStatusChangePermittedResponse } from '../lsp/protocol';
import { Commands } from '../util/commands';
import { isValidRange, LocationTreeItem, SecondaryLocationsTree } from '../location/locations';
import * as protocol from '../lsp/protocol';
import { DateTime } from 'luxon';
Expand Down Expand Up @@ -103,19 +102,9 @@ export class IssueService {

static async showIssue(issue: protocol.Issue) {
const documentUri = protocol2CodeConverter(issue.fileUri);
if (documentUri == null) {
VSCode.window
.showErrorMessage(
`Could not find file '${issue.fileUri}' in the current workspace.
Please make sure that the right folder is open and bound to the right project on the server,
and that the file has not been removed or renamed.`,
'Show Documentation'
)
.then(action => {
if (action === 'Show Documentation') {
VSCode.commands.executeCommand(Commands.OPEN_BROWSER, VSCode.Uri.parse('https://docs.sonarsource.com/sonarlint/vs-code/troubleshooting/#no-matching-issue-found'));
}
});
const exists = await pathExists(documentUri);
if (documentUri == null || !exists) {
showNoFileWithUriError(documentUri);
} else {
const editor = await VSCode.window.showTextDocument(documentUri);

Expand Down
16 changes: 16 additions & 0 deletions src/util/showMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import * as vscode from 'vscode';
import { window } from 'vscode';
import { SslCertificateConfirmationParams } from '../lsp/protocol';
import { Commands } from '../util/commands';

const OPEN_FOLDER_ACTION = 'Open Folder';
export const DONT_ASK_AGAIN_ACTION = "Don't Ask Again";
Expand Down Expand Up @@ -88,3 +89,18 @@ export async function showSslCertificateConfirmationDialog(cert: SslCertificateC
export function showNoActiveFileOpenWarning() {
return window.showWarningMessage('At least one file needs to be open to use this command');
}

export function showNoFileWithUriError(uri: vscode.Uri) {
vscode.window
.showErrorMessage(
`Could not find file '${uri}' in the current workspace.
Please make sure that the right folder is open and bound to the right project on the server,
and that the file has not been removed or renamed.`,
'Show Documentation'
)
.then(action => {
if (action === 'Show Documentation') {
vscode.commands.executeCommand(Commands.OPEN_BROWSER, vscode.Uri.parse('https://docs.sonarsource.com/sonarlint/vs-code/troubleshooting/#no-matching-issue-found'));
}
});
}
9 changes: 9 additions & 0 deletions src/util/uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,12 @@ export function getUriFromRelativePath(relativePath: string, workspaceFolder: vs
const workspaceFolderUri = workspaceFolder.uri;
return `${workspaceFolderUri}/${relativePath}`;
}

export async function pathExists(uri: vscode.Uri): Promise<boolean> {
try {
await vscode.workspace.fs.stat(uri);
return true;
} catch (e) {
return false;
}
}

0 comments on commit c383ada

Please sign in to comment.