Skip to content

Commit

Permalink
feat: refactor round 3
Browse files Browse the repository at this point in the history
also removed activeQP requirement and utilises setCursorPos as well
  • Loading branch information
joshmu committed Apr 27, 2024
1 parent d6cf169 commit ea9bff3
Show file tree
Hide file tree
Showing 8 changed files with 446 additions and 444 deletions.
14 changes: 7 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import * as vscode from 'vscode';
import { periscope } from './lib/periscope';
import { openInHorizontalSplit } from './lib/editorActions';
import { PERISCOPE } from './lib/periscope';
import { log } from './utils/log';

export function activate(context: vscode.ExtensionContext) {
console.log('<<PERISCOPE>> is now active.');
log('activate');

const periscopeQpCmd = vscode.commands.registerCommand('periscope.search',
() => periscope().register()
() => PERISCOPE.search()
);

const periscopeSplitCmd = vscode.commands.registerCommand("periscope.openInHorizontalSplit", () => {
openInHorizontalSplit();
});
const periscopeSplitCmd = vscode.commands.registerCommand("periscope.openInHorizontalSplit",
() => PERISCOPE.openInHorizontalSplit()
);

context.subscriptions.push(periscopeQpCmd, periscopeSplitCmd);
}
59 changes: 50 additions & 9 deletions src/lib/editorActions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as vscode from 'vscode';
import * as path from 'path';
import { previousActiveEditor, updatePreviousActiveEditor } from './editorContext';
import { activeQP } from './quickpickContext';
import { AllQPItemVariants, QPItemQuery } from '../types';
import {context as cx} from './context';
import { context as cx } from './context';

export function closePreviewEditor() {
if(previousActiveEditor) {
Expand All @@ -13,12 +13,12 @@ export function closePreviewEditor() {

// Open the current qp selected item in a horizontal split
export const openInHorizontalSplit = () => {
if(!activeQP) {
if(!cx.qp) {
return;
}

// grab the current selected item
const currentItem = activeQP.activeItems[0] as QPItemQuery;
const currentItem = cx.qp.activeItems[0] as QPItemQuery;

if (!currentItem?.data) {
return;
Expand All @@ -32,11 +32,9 @@ export const openInHorizontalSplit = () => {

const { filePath, linePos, colPos } = currentItem.data;
vscode.workspace.openTextDocument(filePath).then((document) => {
vscode.window.showTextDocument(document, options).then((editor) => {
// set cursor & view position
const position = new vscode.Position(linePos, colPos);
editor.revealRange(new vscode.Range(position, position));
activeQP?.dispose();
vscode.window.showTextDocument(document, options).then(editor => {
setCursorPosition(editor, linePos, colPos);
cx.qp?.dispose();
});
});
};
Expand Down Expand Up @@ -80,3 +78,46 @@ export function openNativeVscodeSearch(query: string, qp: vscode.QuickPick<AllQP
cx.highlightDecoration.set(editor);
});
}


export function handleNoResultsFound() {
if (cx.config.showPreviousResultsWhenNoMatches) {
return;
}

// hide the previous results if no results found
cx.qp.items = [];
// no peek preview available, show the origin document instead
showPreviewOfOriginDocument();
}

export function showPreviewOfOriginDocument() {
if (!previousActiveEditor) {return;}
vscode.window.showTextDocument(previousActiveEditor.document, {
preserveFocus: true,
preview: true
});
}

export function peekItem(items: readonly QPItemQuery[]) {
if (items.length === 0) {
return;
}

const currentItem = items[0];
if (!currentItem.data) {
return;
}

const { filePath, linePos, colPos } = currentItem.data;
vscode.workspace.openTextDocument(path.resolve(filePath)).then(document => {
vscode.window
.showTextDocument(document, {
preview: true,
preserveFocus: true,
})
.then(editor => {
setCursorPosition(editor, linePos, colPos);
});
});
}
53 changes: 53 additions & 0 deletions src/lib/globalActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as vscode from 'vscode';
import * as path from 'path';
import { log } from "../utils/log";
import { updatePreviousActiveEditor } from "./editorContext";
import { checkKillProcess } from "./ripgrep";
import { context as cx } from './context';
import { QPItemQuery } from '../types';
import { closePreviewEditor, setCursorPosition } from './editorActions';

export function accept(item?: QPItemQuery) {
checkKillProcess();

const currentItem = item ? item : cx.qp.selectedItems[0] as QPItemQuery;
if (!currentItem?.data) {
return;
}

const { filePath, linePos, colPos } = currentItem.data;
vscode.workspace.openTextDocument(path.resolve(filePath)).then(document => {
const options: vscode.TextDocumentShowOptions = {};

if(item) { // horizontal split
options.viewColumn = vscode.ViewColumn.Beside;
closePreviewEditor();
}

vscode.window.showTextDocument(document, options).then(editor => {
setCursorPosition(editor, linePos, colPos);
cx.qp.dispose();
});
});
}

export function finished() {
checkKillProcess();
cx.highlightDecoration.remove();
setActiveContext(false);
disposeAll();
updatePreviousActiveEditor(undefined);
log('finished');
}

// create vscode context for the extension for targeted keybindings
export function setActiveContext(flag: boolean) {
log(`setContext ${flag}`);
vscode.commands.executeCommand('setContext', 'periscopeActive', flag);
}

function disposeAll() {
cx.disposables.general.forEach(d => d.dispose());
cx.disposables.rgMenuActions.forEach(d => d.dispose());
cx.disposables.query.forEach(d => d.dispose());
}
Loading

0 comments on commit ea9bff3

Please sign in to comment.