Skip to content

Commit 0d2fab7

Browse files
authored
Merge pull request #47 from MetabobProject/fix/path-sending
fix file path not being sent to backend
2 parents 83daf75 + 3d82681 commit 0d2fab7

File tree

10 files changed

+63
-61
lines changed

10 files changed

+63
-61
lines changed

ext-src/commands/DetailDocument.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function activateDetailSuggestionCommand(context: vscode.ExtensionContext
2222
const sessionToken = new Session(context).get()?.value;
2323
const extensionEventEmitter = getExtensionEventEmitter();
2424

25-
const documentMetaData = Utils.getFileNameFromCurrentEditor();
25+
const documentMetaData = Utils.getCurrentFile();
2626

2727
if (!documentMetaData) {
2828
vscode.window.showErrorMessage(CONSTANTS.editorNotSelectorError);

ext-src/commands/DiscardSuggestion.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ export function activateDiscardCommand(context: vscode.ExtensionContext): void {
3333
}
3434

3535
// verifying that in-fact user viewing problem.path file.
36-
const documentMetaData = Utils.getFileNameFromCurrentEditor();
36+
const documentMetaData = Utils.getCurrentFile();
3737
if (!documentMetaData) {
3838
vscode.window.showErrorMessage(CONSTANTS.editorNotSelectorError);
3939
return;
4040
}
4141

42-
const filename: string | undefined = documentMetaData.fileName;
42+
const filename: string | undefined = documentMetaData.absPath;
4343
const isUserOnProblemFile: boolean = filename === path;
4444

4545
const copyProblems = { ...problems };

ext-src/extension.ts

+10-16
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export function activate(context: vscode.ExtensionContext): void {
145145
return;
146146
}
147147

148-
const documentMetaData = Util.getFileNameFromCurrentEditor();
148+
const documentMetaData = Util.getCurrentFile();
149149

150150
if (!documentMetaData) {
151151
return;
@@ -257,28 +257,22 @@ export function activate(context: vscode.ExtensionContext): void {
257257
}
258258

259259
const currentWorkSpaceFolder = Util.getRootFolderName();
260-
const documentMetaData = Util.extractMetaDataFromDocument(bufferedEParam);
261-
let fileName: string | undefined = undefined;
262-
263-
if (documentMetaData.fileName) {
264-
fileName = documentMetaData.fileName;
260+
const currentWorkspacePath = vscode.workspace.workspaceFolders?.[0].uri.fsPath;
261+
if (currentWorkspacePath === undefined) {
262+
return;
265263
}
266264

267-
if (!fileName && documentMetaData.filePath) {
268-
const splitKey: string | undefined = documentMetaData.filePath
269-
.split('/')
270-
.pop()
271-
?.replace('.git', '');
272-
if (splitKey) {
273-
fileName = splitKey;
274-
}
265+
const documentMetaData = Util.extractMetaDataFromDocument(bufferedEParam);
266+
if (!documentMetaData.filePath) {
267+
return
275268
}
269+
const filePath: string = documentMetaData.filePath.replace(/\.git$/, '');
276270

277-
if (!fileName) {
271+
if (!filePath) {
278272
return;
279273
}
280274

281-
const results: Problem[] | undefined = Util.getCurrentEditorProblems(analyzeValue, fileName);
275+
const results: Problem[] | undefined = Util.getCurrentEditorProblems(analyzeValue, filePath);
282276
if (!results) {
283277
return;
284278
}

ext-src/helpers/HandleDocumentAnalyze.ts

+19-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { AnalyzeState, Analyze, AnalyseMetaData } from '../state';
66
import Util from '../utils';
77
import CONSTANTS from '../constants';
88
import { getExtensionEventEmitter } from '../events';
9+
import path from 'path'
910

1011
const failedResponseReturn: SubmitRepresentationResponse = { jobId: '', status: 'failed' };
1112

@@ -101,8 +102,8 @@ export const handleDocumentAnalyze = async (
101102
return verifiedResponse;
102103
}
103104

104-
const documentMetaData = Util.getFileNameFromCurrentEditor();
105-
if (!documentMetaData) {
105+
const currFile = Util.getCurrentFile();
106+
if (!currFile) {
106107
getExtensionEventEmitter().fire({
107108
type: 'Analysis_Error',
108109
data: '',
@@ -118,20 +119,26 @@ export const handleDocumentAnalyze = async (
118119
return failedResponseReturn;
119120
}
120121

122+
// convert problem paths to absolute path and normalize them
123+
const workspaceFolderPath = vscode.workspace.workspaceFolders?.[0].uri.fsPath;
124+
if (!workspaceFolderPath) {return failedResponseReturn;}
125+
verifiedResponse.results.forEach(result => {
126+
result.path = path.join(workspaceFolderPath, result.path);
127+
});
128+
121129
let results: AnalyzeState = {};
122130
const analyzeStateValue = new Analyze(context).get()?.value;
123131
_debug?.appendLine('AnalyzeDocument.ts: handleDocumentAnalyze: analyzeStateValue: ' + JSON.stringify(analyzeStateValue));
124132

125133
if (analyzeStateValue) {
126-
const aggregatedProblemsFilePaths = verifiedResponse.results.map(problem => {
134+
const responseProblemsFilePaths = verifiedResponse.results.map(problem => {
127135
return problem.path;
128136
});
129137

130138
const buggerAnalyzeStateValue: AnalyzeState = {};
131139

132-
Object.keys(analyzeStateValue).forEach(key => {
133-
const problem = analyzeStateValue[key];
134-
if (!aggregatedProblemsFilePaths.includes(problem.path)) {
140+
Object.entries(analyzeStateValue).forEach(([key, problem]) => {
141+
if (!responseProblemsFilePaths.includes(problem.path)) {
135142
buggerAnalyzeStateValue[key] = { ...problem };
136143
}
137144
});
@@ -153,10 +160,10 @@ export const handleDocumentAnalyze = async (
153160
startLine - 1,
154161
0,
155162
endLine - 1,
156-
documentMetaData.editor.document.lineAt(endLine - 1).text.length,
163+
currFile.editor.document.lineAt(endLine - 1).text.length,
157164
);
158165

159-
const text = documentMetaData.editor.document
166+
const text = currFile.editor.document
160167
.getText(range)
161168
.replace('\n', '')
162169
.replace('\t', '');
@@ -181,8 +188,8 @@ export const handleDocumentAnalyze = async (
181188
results[key] = { ...analyzeMetaData };
182189
});
183190

184-
_debug?.appendLine('AnalyzeDocument.ts: Document File name' + documentMetaData.fileName);
185-
const problems = Util.getCurrentEditorProblems(results, documentMetaData.fileName);
191+
_debug?.appendLine('AnalyzeDocument.ts: Document File path' + currFile.absPath);
192+
const problems = Util.getCurrentEditorProblems(results, currFile.absPath);
186193
_debug?.appendLine('AnalyzeDocument.ts: handleDocumentAnalyze: problems: ' + JSON.stringify(problems));
187194
if (!problems) {
188195
getExtensionEventEmitter().fire({
@@ -202,10 +209,10 @@ export const handleDocumentAnalyze = async (
202209

203210
const paths = problems.map(item => item.path);
204211

205-
const isUserOnValidEditor = paths.includes(documentMetaData.fileName);
212+
const isUserOnValidEditor = paths.includes(currFile.absPath);
206213
_debug?.appendLine('AnalyzeDocument.ts: handleDocumentAnalyze: isUserOnValidEditor: ' + isUserOnValidEditor);
207214
if (isUserOnValidEditor) {
208-
Util.decorateCurrentEditorWithHighlights(problems, documentMetaData.editor, _debug);
215+
Util.decorateCurrentEditorWithHighlights(problems, currFile.editor, _debug);
209216
}
210217

211218
await analyzeState.set(results);

ext-src/providers/recommendation.provider.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -425,13 +425,13 @@ export class RecommendationWebView implements WebviewViewProvider {
425425
}
426426

427427
async handleApplyRecommendation(input: string, initData: CurrentQuestionState) {
428-
const documentMetadata = Util.getFileNameFromCurrentEditor();
428+
const documentMetadata = Util.getCurrentFile();
429429
if (!documentMetadata || !initData) {
430430
throw new Error('handleApplyRecommendation: Editor or Init Data is undefined');
431431
}
432432

433433
const key = `${initData.path}@@${initData.id}`;
434-
if (documentMetadata.fileName !== initData.path) {
434+
if (documentMetadata.absPath !== initData.path) {
435435
throw new Error('handleApplyRecommendation: User editor changed');
436436
}
437437

@@ -484,16 +484,15 @@ export class RecommendationWebView implements WebviewViewProvider {
484484

485485
searchFileByName(fileName: string) {
486486
const searchPattern = `**/${fileName}`;
487+
487488
return workspace.findFiles(searchPattern, '**/node_modules/**', 1);
488489
}
489490

490-
async openFileInNewTab(fileName: string) {
491-
const searchedFilePath = await this.searchFileByName(fileName);
492-
const path: Uri | undefined = searchedFilePath[0];
491+
async openFileInNewTab(filePath: string): Promise<void> {
492+
if (!filePath) return;
493493

494-
if (!path) return;
495494
// Use the `openTextDocument` method to open the document
496-
workspace.openTextDocument(Uri.file(path.fsPath)).then(document => {
495+
workspace.openTextDocument(Uri.file(filePath)).then(document => {
497496
// Use the `showTextDocument` method to show the document in a new tab
498497
window.showTextDocument(document, {
499498
viewColumn: ViewColumn.One,
@@ -516,8 +515,8 @@ export class RecommendationWebView implements WebviewViewProvider {
516515
const data = message.data;
517516
switch (message.type) {
518517
case 'OPEN_FILE_IN_NEW_TAB':
519-
const { name: fileName } = data;
520-
this.openFileInNewTab(fileName);
518+
const { path: filePath } = data;
519+
this.openFileInNewTab(filePath);
521520
break;
522521
case 'analysis_current_file':
523522
this.clear();

ext-src/services/submit/submit.service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class SubmitService extends ApiServiceBase {
2525
const formData = new FormData()
2626
formData.append('type', 'text/plain')
2727
formData.append('filename', relativePath)
28-
formData.append('upload', Buffer.from(fileContent, 'utf-8'), relativePath)
28+
formData.append('upload', Buffer.from(fileContent, 'utf-8'), {filepath: relativePath})
2929
const config = this.getConfig(sessionToken, formData.getHeaders())
3030
const response = await this.post<SubmitRepresentationResponse>('/submit', formData, config)
3131
return response

ext-src/utils.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ export default class Utils {
145145
return undefined; // No workspace folder
146146
}
147147

148-
static getFileNameFromCurrentEditor(): {
148+
static getCurrentFile(): {
149149
fileName: string;
150+
absPath: string;
150151
editor: vscode.TextEditor
151152
} | undefined {
152153
const editor = vscode.window.activeTextEditor
@@ -161,19 +162,19 @@ export default class Utils {
161162

162163
return {
163164
fileName,
165+
absPath: documentMetaData.filePath,
164166
editor
165167
}
166168
}
167169

168-
static getCurrentEditorProblems(analyzeValue: AnalyzeState, problemFileName: string): Problem[] | undefined {
170+
static getCurrentEditorProblems(analyzeValue: AnalyzeState, currentFilePath: string): Problem[] | undefined {
169171
const results: Problem[] = [];
170172

171-
for (const [key, value] of Object.entries(analyzeValue)) {
172-
const fileNameFromKey: string | undefined = key.split('@@')[0];
173-
if (fileNameFromKey === undefined) continue;
173+
for (const value of Object.values(analyzeValue)) {
174+
if (value.path === undefined) continue;
174175

175176
// verifying that we only show current opened file decorations that are not discarded.
176-
if (fileNameFromKey === problemFileName && value.isDiscarded === false) {
177+
if (value.path === currentFilePath && value.isDiscarded === false) {
177178
const problem: Problem = {
178179
...value,
179180
startLine: value.startLine < 0 ? value.startLine * -1 : value.startLine,

src/components/Analyze/ProblemList/index.spec.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('ProblemList component', () => {
3737
});
3838

3939
test('renders correctly with otherFileWithProblems', () => {
40-
const otherFileWithProblems = [{ name: 'file1.txt' }, { name: 'file2.txt' }];
40+
const otherFileWithProblems = [{ name: 'file1.txt', path: 'path/to/file1.txt' }, { name: 'file2.txt', path: 'path/to/file2.txt' }];
4141
const { getByText, getAllByText } = render(
4242
<RecoilRoot>
4343
<ProblemList
@@ -55,7 +55,7 @@ describe('ProblemList component', () => {
5555
});
5656

5757
test('handles opening other files correctly', () => {
58-
const otherFileWithProblems = [{ name: 'file1.txt' }];
58+
const otherFileWithProblems = [{ name: 'file1.txt', path: 'path/to/file1.txt' }];
5959
const { getByText } = render(
6060
<RecoilRoot>
6161
<ProblemList
@@ -69,7 +69,7 @@ describe('ProblemList component', () => {
6969
fireEvent.click(getByText('Open'));
7070
expect(vscode.postMessage).toHaveBeenCalledWith({
7171
type: 'OPEN_FILE_IN_NEW_TAB',
72-
data: { name: 'file1.txt' },
72+
data: { path: 'path/to/file1.txt' },
7373
});
7474
});
7575
});

src/components/Analyze/ProblemList/index.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { useCallback } from 'react';
1313

1414
export interface ProblemsListProps {
1515
detectedProblems?: number;
16-
otherFileWithProblems?: Array<{ name: string }>;
16+
otherFileWithProblems?: Array<{ name: string, path: string }>;
1717
isEmptyIdentifiedProblemDetected: boolean;
1818
}
1919

@@ -24,12 +24,12 @@ export const ProblemList = ({
2424
}: ProblemsListProps): JSX.Element => {
2525
const theme = useTheme();
2626

27-
const handleOpenOtherFile: (name: string) => React.MouseEventHandler = useCallback(
28-
(name: string) => e => {
27+
const handleOpenOtherFile: (path: string) => React.MouseEventHandler = useCallback(
28+
(path: string) => e => {
2929
e.preventDefault();
3030
vscode.postMessage({
3131
type: 'OPEN_FILE_IN_NEW_TAB',
32-
data: { name },
32+
data: { path },
3333
});
3434
},
3535
[],
@@ -75,7 +75,7 @@ export const ProblemList = ({
7575
size='small'
7676
variant='contained'
7777
color='primary'
78-
onClick={handleOpenOtherFile(item.name)}
78+
onClick={handleOpenOtherFile(item.path)}
7979
>
8080
Open
8181
</Button>

src/components/Analyze/index.tsx

+7-6
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ export const AnalyzePage = ({
2626
const currentWorkSpaceProject = useRecoilValue(State.currentWorkSpaceProject);
2727
const isEmptyIdentifiedProblemDetected = useRecoilValue(State.isEmptyIdentifiedProblemDetected);
2828

29-
const otherFileWithProblems: Array<{ name: string }> | undefined = useMemo(() => {
29+
const otherFileWithProblems: Array<{ name: string, path: string }> | undefined = useMemo(() => {
3030
if (!identifiedProblems) return undefined;
3131

3232
if (!currentEditor) return undefined;
3333

34-
const results: Array<{ name: string }> = Object.keys(identifiedProblems)
34+
const results: Array<{ path: string }> = Object.keys(identifiedProblems)
3535
.filter(problemKey => {
3636
const problem = identifiedProblems[problemKey];
3737

@@ -74,15 +74,16 @@ export const AnalyzePage = ({
7474
const problem = identifiedProblems[problemKey];
7575

7676
return {
77-
name: problem.path,
77+
path: problem.path,
7878
};
7979
});
8080

81-
const uniqueFiles = Array.from(new Set(results.map(item => item.name)));
81+
const uniqueFiles = Array.from(new Set(results.map(item => item.path)));
8282

83-
return uniqueFiles.map(name => {
83+
return uniqueFiles.map(file => {
8484
return {
85-
name,
85+
path: file,
86+
name: file.split(/\/|\\/g).pop() || '',
8687
};
8788
});
8889
}, [identifiedProblems, currentEditor, currentWorkSpaceProject]);

0 commit comments

Comments
 (0)