|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as vscode from 'vscode'; |
| 3 | + |
| 4 | +import { getAndUpdateModeHandler } from '../../extension'; |
| 5 | +import { GrepCommand } from '../../src/cmd_line/commands/grep'; |
| 6 | +import { Pattern, SearchDirection } from '../../src/vimscript/pattern'; |
| 7 | +import { Mode } from '../../src/mode/mode'; |
| 8 | +import { createFile, setupWorkspace, cleanUpWorkspace } from '../testUtils'; |
| 9 | + |
| 10 | +function grep(pattern: Pattern, files: string[]): GrepCommand { |
| 11 | + return new GrepCommand({ pattern, files }); |
| 12 | +} |
| 13 | + |
| 14 | +suite('Basic grep command', () => { |
| 15 | + // when you search.action.focusNextSearchResult , it will enter the file in visual mode for some reason, we can test whether it is in visual mode or not after running that command |
| 16 | + // that only happens if the search panel is not open already |
| 17 | + // if the search panel is open, it will be in normal mode |
| 18 | + // it will also be in normal mode if you run vimgrep from another file |
| 19 | + setup(async () => { |
| 20 | + await setupWorkspace(); |
| 21 | + }); |
| 22 | + test('GrepCommand executes correctly', async () => { |
| 23 | + // first file, will have matches |
| 24 | + let file1 = await createFile({ |
| 25 | + fileExtension: '.txt', |
| 26 | + contents: 'test, pattern nnnn, t*st, ttst', |
| 27 | + }); |
| 28 | + // second file without a match |
| 29 | + let file2 = await createFile({ |
| 30 | + fileExtension: '.txt', |
| 31 | + contents: 'no pattern match here ', |
| 32 | + }); |
| 33 | + // We open the second file where we know there is no match |
| 34 | + const document1 = await vscode.workspace.openTextDocument(vscode.Uri.file(file1)); |
| 35 | + await vscode.window.showTextDocument(document1, { preview: false }); |
| 36 | + const document2 = await vscode.workspace.openTextDocument(vscode.Uri.file(file2)); |
| 37 | + await vscode.window.showTextDocument(document2, { preview: false }); |
| 38 | + const pattern = Pattern.parser({ direction: SearchDirection.Backward }); |
| 39 | + // The vscode's search doesn't work with the paths of the extension test host, so we strip to the file names only |
| 40 | + file1 = file1.substring(file1.lastIndexOf('/') + 1); |
| 41 | + file2 = file2.substring(file2.lastIndexOf('/') + 1); |
| 42 | + const command = grep(pattern.tryParse('t*st'), [file1, file2]); |
| 43 | + await command.execute(); |
| 44 | + // Despite the fact that we already execute this command in the grep itself, without this focus, there is no active editor |
| 45 | + // I've tested visually and without this command you are still in the editor in the file with the match, I have no idea why it won't work without this |
| 46 | + await vscode.commands.executeCommand('search.action.focusNextSearchResult'); |
| 47 | + const activeEditor = vscode.window.activeTextEditor; |
| 48 | + const modeHandler = await getAndUpdateModeHandler(); |
| 49 | + assert.ok(activeEditor, 'There should be an active editor'); |
| 50 | + assert.ok(modeHandler, 'modeHandler should be defined'); |
| 51 | + const docs = vscode.workspace.textDocuments.map((doc) => doc.fileName); |
| 52 | + // After grep, the active editor should be the first file because the search panel focuses the first match and therefore opens the file |
| 53 | + assert.ok( |
| 54 | + activeEditor.document.fileName.endsWith(file1), |
| 55 | + 'Active editor should be first file after grep', |
| 56 | + ); |
| 57 | + assert.notStrictEqual( |
| 58 | + modeHandler.vimState, |
| 59 | + Mode.Visual, |
| 60 | + 'Should not be in visual mode after grep', |
| 61 | + ); |
| 62 | + }); |
| 63 | +}); |
0 commit comments