Skip to content

Commit

Permalink
feat: refactor round 1
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmu committed Apr 26, 2024
1 parent e6b2332 commit 557ab82
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 161 deletions.
3 changes: 2 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode';
import { openInHorizontalSplit, periscope } from './periscope';
import { periscope } from './lib/periscope';
import { openInHorizontalSplit } from './lib/editorActions';

export function activate(context: vscode.ExtensionContext) {
console.log('<<PERISCOPE>> is now active.');
Expand Down
83 changes: 83 additions & 0 deletions src/lib/editorActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import * as vscode from 'vscode';
import { previousActiveEditor, updatePreviousActiveEditor } from './editorContext';
import { activeQP } from './quickpickContext';
import { AllQPItemVariants, QPItemQuery } from '../types';
import { getConfig } from '../utils/getConfig';
import { highlightLineDecorationType } from '../utils/decorationType';

export function closePreviewEditor() {
if(previousActiveEditor) {
vscode.commands.executeCommand('workbench.action.closeActiveEditor');
updatePreviousActiveEditor(undefined); // prevent focus onDidHide
}
}

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

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

if (!currentItem?.data) {
return;
}

const options: vscode.TextDocumentShowOptions = {
viewColumn: vscode.ViewColumn.Beside,
};

closePreviewEditor();

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();
});
});
};

// Open the native VSCode search with the provided query and enable regex
export function openNativeVscodeSearch(query: string, qp: vscode.QuickPick<AllQPItemVariants>) {
// remove the config suffix from the query
const trimmedQuery = query.slice(
0,
query.indexOf(getConfig().gotoNativeSearchSuffix)
);

vscode.commands.executeCommand('workbench.action.findInFiles', {
query: trimmedQuery,
isRegex: true,
isCaseSensitive: false,
matchWholeWord: false,
triggerSearch: true,
});

// close extension down
qp.hide();
}

export function setCursorPosition(editor: vscode.TextEditor, linePos: number, colPos: number) {
const selection = new vscode.Selection(0, 0, 0, 0);
editor.selection = selection;

const lineNumber = Math.max(linePos ? linePos - 1 : 0, 0);
const charNumber = Math.max(colPos ? colPos - 1 : 0, 0);

editor
.edit(editBuilder => {
editBuilder.insert(selection.active, '');
})
.then(() => {
const newPosition = new vscode.Position(lineNumber, charNumber);
const range = editor.document.lineAt(newPosition).range;
editor.selection = new vscode.Selection(newPosition, newPosition);
editor.revealRange(range, vscode.TextEditorRevealType.InCenter);
highlightLineDecorationType().set(editor);
});
}
8 changes: 8 additions & 0 deletions src/lib/editorContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// lib/editorContext.ts
import * as vscode from 'vscode';

export let previousActiveEditor: vscode.TextEditor | undefined;

export function updatePreviousActiveEditor(editor: vscode.TextEditor | undefined) {
previousActiveEditor = editor;
}
Loading

0 comments on commit 557ab82

Please sign in to comment.