Skip to content

Commit

Permalink
Mark a few more arrays in vscode.d.ts readonly
Browse files Browse the repository at this point in the history
This makes it clear that you can't modify these arrays to add items, such as calling `breakpoints.push(...)` to add a breakpoint
  • Loading branch information
mjbvz committed Nov 11, 2021
1 parent 1423849 commit b911788
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion extensions/emmet/src/abbreviationActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function wrapWithAbbreviation(args: any): Promise<boolean> {

const helper = getEmmetHelper();

const operationRanges = editor.selections.sort((a, b) => a.start.compareTo(b.start)).map(selection => {
const operationRanges = Array.from(editor.selections).sort((a, b) => a.start.compareTo(b.start)).map(selection => {
let rangeToReplace: vscode.Range = selection;
// wrap around the node if the selection falls inside its open or close tag
{
Expand Down
12 changes: 5 additions & 7 deletions extensions/emmet/src/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { getHtmlFlatNode, offsetRangeToSelection, validate } from './util';
import { getRootNode } from './parseDocument';
import { HtmlNode as HtmlFlatNode } from 'EmmetFlatNode';

let balanceOutStack: Array<vscode.Selection[]> = [];
let lastBalancedSelections: vscode.Selection[] = [];
let balanceOutStack: Array<readonly vscode.Selection[]> = [];
let lastBalancedSelections: readonly vscode.Selection[] = [];

export function balanceOut() {
balance(true);
Expand All @@ -31,10 +31,8 @@ function balance(out: boolean) {
}

const rangeFn = out ? getRangeToBalanceOut : getRangeToBalanceIn;
let newSelections: vscode.Selection[] = [];
editor.selections.forEach(selection => {
const range = rangeFn(document, rootNode, selection);
newSelections.push(range);
let newSelections: readonly vscode.Selection[] = editor.selections.map(selection => {
return rangeFn(document, rootNode, selection);
});

// check whether we are starting a balance elsewhere
Expand Down Expand Up @@ -122,7 +120,7 @@ function getRangeToBalanceIn(document: vscode.TextDocument, rootNode: HtmlFlatNo
return offsetRangeToSelection(document, firstChild.start, firstChild.end);
}

function areSameSelections(a: vscode.Selection[], b: vscode.Selection[]): boolean {
function areSameSelections(a: readonly vscode.Selection[], b: readonly vscode.Selection[]): boolean {
if (a.length !== b.length) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion extensions/emmet/src/mergeLines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function mergeLines() {
}

return editor.edit(editBuilder => {
editor.selections.reverse().forEach(selection => {
Array.from(editor.selections).reverse().forEach(selection => {
const textEdit = getRangesToReplace(editor.document, selection, rootNode);
if (textEdit) {
editBuilder.replace(textEdit.range, textEdit.newText);
Expand Down
2 changes: 1 addition & 1 deletion extensions/emmet/src/removeTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function removeTag() {
return;
}

let finalRangesToRemove = editor.selections.reverse()
let finalRangesToRemove = Array.from(editor.selections).reverse()
.reduce<vscode.Range[]>((prev, selection) =>
prev.concat(getRangesToRemove(editor.document, rootNode, selection)), []);

Expand Down
2 changes: 1 addition & 1 deletion extensions/emmet/src/splitJoinTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function splitJoinTag() {
}

return editor.edit(editBuilder => {
editor.selections.reverse().forEach(selection => {
Array.from(editor.selections).reverse().forEach(selection => {
const documentText = document.getText();
const offset = document.offsetAt(selection.start);
const nodeToUpdate = getHtmlFlatNode(documentText, rootNode, offset, true);
Expand Down
2 changes: 1 addition & 1 deletion extensions/emmet/src/toggleComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function toggleComment(): Thenable<boolean> | undefined {

return editor.edit(editBuilder => {
let allEdits: vscode.TextEdit[][] = [];
editor.selections.reverse().forEach(selection => {
Array.from(editor.selections).reverse().forEach(selection => {
const edits = isStyleSheet(editor.document.languageId) ? toggleCommentStylesheet(editor.document, selection, <Stylesheet>rootNode) : toggleCommentHTML(editor.document, selection, rootNode!);
if (edits.length > 0) {
allEdits.push(edits);
Expand Down
2 changes: 1 addition & 1 deletion extensions/emmet/src/updateImageSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function updateImageSize(): Promise<boolean> | undefined {
}
const editor = window.activeTextEditor;

const allUpdatesPromise = editor.selections.reverse().map(selection => {
const allUpdatesPromise = Array.from(editor.selections).reverse().map(selection => {
const position = selection.isReversed ? selection.active : selection.anchor;
if (!isStyleSheet(editor.document.languageId)) {
return updateImageSizeHTML(editor, position);
Expand Down
2 changes: 1 addition & 1 deletion extensions/emmet/src/updateTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function updateTag(tagName: string | undefined): Promise<boolean |
return;
}

const rangesToUpdate = editor.selections.reverse()
const rangesToUpdate = Array.from(editor.selections).reverse()
.reduce<TagRange[]>((prev, selection) =>
prev.concat(getRangesToUpdate(document, selection, rootNode)), []);
if (!rangesToUpdate.length) {
Expand Down
2 changes: 1 addition & 1 deletion extensions/git/src/staging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function applyLineChanges(original: TextDocument, modified: TextDocument,
return result.join('');
}

export function toLineRanges(selections: Selection[], textDocument: TextDocument): Range[] {
export function toLineRanges(selections: readonly Selection[], textDocument: TextDocument): Range[] {
const lineRanges = selections.map(s => {
const startLine = textDocument.lineAt(s.start.line);
const endLine = textDocument.lineAt(s.end.line);
Expand Down
18 changes: 9 additions & 9 deletions src/vs/vscode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1107,13 +1107,13 @@ declare module 'vscode' {
/**
* The selections in this text editor. The primary selection is always at index 0.
*/
selections: Selection[];
selections: readonly Selection[];

/**
* The current visible ranges in the editor (vertically).
* This accounts only for vertical scrolling, and not for horizontal scrolling.
*/
readonly visibleRanges: Range[];
readonly visibleRanges: readonly Range[];

/**
* Text editor options.
Expand Down Expand Up @@ -8656,7 +8656,7 @@ declare module 'vscode' {
/**
* The currently visible editors or an empty array.
*/
export let visibleTextEditors: TextEditor[];
export let visibleTextEditors: readonly TextEditor[];

/**
* An {@link Event} which fires when the {@link window.activeTextEditor active editor}
Expand All @@ -8669,7 +8669,7 @@ declare module 'vscode' {
* An {@link Event} which fires when the array of {@link window.visibleTextEditors visible editors}
* has changed.
*/
export const onDidChangeVisibleTextEditors: Event<TextEditor[]>;
export const onDidChangeVisibleTextEditors: Event<readonly TextEditor[]>;

/**
* An {@link Event} which fires when the selection in an editor has changed.
Expand Down Expand Up @@ -9354,7 +9354,7 @@ declare module 'vscode' {
/**
* Selected elements.
*/
readonly selection: T[];
readonly selection: readonly T[];

}

Expand Down Expand Up @@ -9388,7 +9388,7 @@ declare module 'vscode' {
/**
* Currently selected elements.
*/
readonly selection: T[];
readonly selection: readonly T[];

/**
* Event that is fired when the {@link TreeView.selection selection} has changed
Expand Down Expand Up @@ -13387,7 +13387,7 @@ declare module 'vscode' {
/**
* List of breakpoints.
*/
export let breakpoints: Breakpoint[];
export let breakpoints: readonly Breakpoint[];

/**
* An {@link Event} which fires when the {@link debug.activeDebugSession active debug session}
Expand Down Expand Up @@ -14322,7 +14322,7 @@ declare module 'vscode' {
* The process of running tests should resolve the children of any test
* items who have not yet been resolved.
*/
readonly include: TestItem[] | undefined;
readonly include: readonly TestItem[] | undefined;

/**
* An array of tests the user has marked as excluded from the test included
Expand All @@ -14331,7 +14331,7 @@ declare module 'vscode' {
* May be omitted if no exclusions were requested. Test controllers should
* not run excluded tests or any children of excluded tests.
*/
readonly exclude: TestItem[] | undefined;
readonly exclude: readonly TestItem[] | undefined;

/**
* The profile used for this request. This will always be defined
Expand Down

0 comments on commit b911788

Please sign in to comment.