Skip to content

Commit

Permalink
fix(general): Fixes #66
Browse files Browse the repository at this point in the history
  • Loading branch information
TimCluyts authored and borislit committed Apr 23, 2019
1 parent 7ba1df4 commit 8d104f4
Showing 1 changed file with 38 additions and 37 deletions.
75 changes: 38 additions & 37 deletions src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as vscode from 'vscode';
import * as path from 'path';
import { QuickPickItem } from 'vscode';


export const workspaceRoot = () => vscode.workspace.rootPath;

export const activeURI = () => vscode.window.activeTextEditor.document.uri;
Expand All @@ -15,62 +14,63 @@ export const activeEditor = () => vscode.window.activeTextEditor;
export const config = () => vscode.workspace.getConfiguration('glean');

export function currentEditorPath(): string {
const activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) return;
const activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) return;

const currentFilePath = path.dirname(activeEditor.document.fileName);
const rootMatcher = new RegExp(`^${workspaceRoot()}`);
const relativeCurrentFilePath = currentFilePath.replace(rootMatcher, '');
const currentFilePath = path.dirname(activeEditor.document.fileName);
const rootMatcher = new RegExp(`^${workspaceRoot()}`);
const relativeCurrentFilePath = currentFilePath.replace(rootMatcher, '');

return relativeCurrentFilePath;
return relativeCurrentFilePath;
}

export function openFile(absolutePath: string): PromiseLike<string> {
return vscode.workspace.openTextDocument(absolutePath)
.then((textDocument) => {
if (textDocument) {
vscode.window.showTextDocument(textDocument);
return absolutePath;
} else {
throw Error('Could not open document');
}
});
return vscode.workspace.openTextDocument(absolutePath).then(textDocument => {
if (textDocument) {
vscode.window.showTextDocument(textDocument);
return absolutePath;
} else {
throw Error('Could not open document');
}
});
}

export function selectedText() {
const editor = vscode.window.activeTextEditor;
const selection = editor.selection;
return editor.document.getText(selection);
const editor = vscode.window.activeTextEditor;
if (editor) {
const selection = editor.selection;
return editor.document.getText(selection);
} else {
return null;
}
}

export function allText() {
const editor = vscode.window.activeTextEditor;
return editor.document.getText();
const editor = vscode.window.activeTextEditor;
return editor.document.getText();
}

export function showInputBox(defaultValue, placeHolder) {
return vscode.window.showInputBox({
value: defaultValue,
placeHolder
});
return vscode.window.showInputBox({
value: defaultValue,
placeHolder
});
}


export function showQuickPicksList(choices: QuickPickItem[], placeHolder = '') {
return vscode.window.showQuickPick<vscode.QuickPickItem>(choices, {
placeHolder
})
};
return vscode.window.showQuickPick<vscode.QuickPickItem>(choices, {
placeHolder
});
}

export const convertRelativeToFullPath = relativePath => {
const root = workspaceRoot();
return root ? path.join(root, relativePath) : relativePath;
}
const root = workspaceRoot();
return root ? path.join(root, relativePath) : relativePath;
};

export const extractQuickPickValue = selection => {
if (!selection)
return;
return selection.label;
if (!selection) return;
return selection.label;
};

export const toQuickPick = (label: string, description?) => ({ label, description });
Expand All @@ -79,4 +79,5 @@ export const toQuickPicksList = (choices: string[]) => choices.map(item => toQui

export const showErrorMessage = message => vscode.window.showErrorMessage(message);

export const showInformationMessage = (message, items: string[] = []) => vscode.window.showInformationMessage(message, ...items);
export const showInformationMessage = (message, items: string[] = []) =>
vscode.window.showInformationMessage(message, ...items);

0 comments on commit 8d104f4

Please sign in to comment.