debug: use .find not .filter .pop()

This commit is contained in:
isidor
2020-05-05 15:35:47 +02:00
parent 177b9c30ea
commit caa19dc534
9 changed files with 28 additions and 30 deletions
@@ -120,7 +120,7 @@ async function createCandidateDecorations(model: ITextModel, breakpointDecoratio
return;
}
const breakpointAtPosition = breakpointDecorations.filter(bpd => bpd.range.equalsRange(range)).pop();
const breakpointAtPosition = breakpointDecorations.find(bpd => bpd.range.equalsRange(range));
if (breakpointAtPosition && breakpointAtPosition.inlineWidget) {
// Space already occupied, do not render candidate.
return;
@@ -473,7 +473,7 @@ class SessionsRenderer implements ITreeRenderer<IDebugSession, FuzzyScore, ISess
const session = element.element;
data.session.title = nls.localize({ key: 'session', comment: ['Session is a noun'] }, "Session");
data.label.set(session.getLabel(), createMatches(element.filterData));
const thread = session.getAllThreads().filter(t => t.stopped).pop();
const thread = session.getAllThreads().find(t => t.stopped);
const setActionBar = () => {
const actions = getActions(this.instantiationService, element.element);
@@ -493,7 +493,7 @@ class SessionsRenderer implements ITreeRenderer<IDebugSession, FuzzyScore, ISess
if (thread && thread.stoppedDetails) {
data.stateLabel.textContent = thread.stoppedDetails.description || nls.localize('debugStopped', "Paused on {0}", thread.stoppedDetails.reason || '');
} else {
const hasChildSessions = this.debugService.getModel().getSessions().filter(s => s.parentSession === session).length > 0;
const hasChildSessions = this.debugService.getModel().getSessions().find(s => s.parentSession === session);
if (!hasChildSessions) {
data.stateLabel.textContent = nls.localize({ key: 'running', comment: ['indicates state'] }, "Running");
} else {
@@ -751,7 +751,7 @@ class CallStackDataSource implements IAsyncDataSource<IDebugModel, CallStackItem
hasChildren(element: IDebugModel | CallStackItem): boolean {
if (isDebugSession(element)) {
const threads = element.getAllThreads();
return (threads.length > 1) || (threads.length === 1 && threads[0].stopped) || (this.debugService.getModel().getSessions().filter(s => s.parentSession === element).length > 0);
return (threads.length > 1) || (threads.length === 1 && threads[0].stopped) || !!(this.debugService.getModel().getSessions().find(s => s.parentSession === element));
}
return isDebugModel(element) || (element instanceof Thread && element.stopped);
@@ -74,7 +74,7 @@ async function getThreadAndRun(accessor: ServicesAccessor, sessionAndThreadId: C
if (isThreadContext(sessionAndThreadId)) {
const session = debugService.getModel().getSession(sessionAndThreadId.sessionId);
if (session) {
thread = session.getAllThreads().filter(t => t.getId() === sessionAndThreadId.threadId).pop();
thread = session.getAllThreads().find(t => t.getId() === sessionAndThreadId.threadId);
}
} else {
thread = debugService.getViewModel().focusedThread;
@@ -98,9 +98,9 @@ function getFrame(debugService: IDebugService, context: CallStackContext | unkno
if (isStackFrameContext(context)) {
const session = debugService.getModel().getSession(context.sessionId);
if (session) {
const thread = session.getAllThreads().filter(t => t.getId() === context.threadId).pop();
const thread = session.getAllThreads().find(t => t.getId() === context.threadId);
if (thread) {
return thread.getCallStack().filter(sf => sf.getId() === context.frameId).pop();
return thread.getCallStack().find(sf => sf.getId() === context.frameId);
}
}
}
@@ -498,7 +498,7 @@ export function registerCommands(): void {
return;
}
const launch = manager.getLaunches().filter(l => l.uri.toString() === launchUri).pop() || manager.selectedConfiguration.launch;
const launch = manager.getLaunches().find(l => l.uri.toString() === launchUri) || manager.selectedConfiguration.launch;
if (launch) {
const { editor, created } = await launch.openConfigFile(false, false);
if (editor && !created) {
@@ -79,7 +79,7 @@ export class ConfigurationManager implements IConfigurationManager {
this.initLaunches();
this.registerListeners();
const previousSelectedRoot = this.storageService.get(DEBUG_SELECTED_ROOT, StorageScope.WORKSPACE);
const previousSelectedLaunch = this.launches.filter(l => l.uri.toString() === previousSelectedRoot).pop();
const previousSelectedLaunch = this.launches.find(l => l.uri.toString() === previousSelectedRoot);
this.debugConfigurationTypeContext = CONTEXT_DEBUG_CONFIGURATION_TYPE.bindTo(contextKeyService);
if (previousSelectedLaunch && previousSelectedLaunch.getConfigurationNames().length) {
this.selectConfiguration(previousSelectedLaunch, this.storageService.get(DEBUG_SELECTED_CONFIG_NAME_KEY, StorageScope.WORKSPACE));
@@ -202,8 +202,8 @@ export class ConfigurationManager implements IConfigurationManager {
triggerKind = DebugConfigurationProviderTriggerKind.Initial;
}
// check if there are providers for the given type that contribute a provideDebugConfigurations method
const providers = this.configProviders.filter(p => p.provideDebugConfigurations && (p.type === debugType) && (p.triggerKind === triggerKind));
return providers.length > 0;
const provider = this.configProviders.find(p => p.provideDebugConfigurations && (p.type === debugType) && (p.triggerKind === triggerKind));
return !!provider;
}
async resolveConfigurationByProviders(folderUri: uri | undefined, type: string | undefined, config: IConfig, token: CancellationToken): Promise<IConfig | null | undefined> {
@@ -416,7 +416,7 @@ export class ConfigurationManager implements IConfigurationManager {
return undefined;
}
return this.launches.filter(l => l.workspace && l.workspace.uri.toString() === workspaceUri.toString()).pop();
return this.launches.find(l => l.workspace && l.workspace.uri.toString() === workspaceUri.toString());
}
get selectedConfiguration(): { launch: ILaunch | undefined, name: string | undefined } {
@@ -490,11 +490,11 @@ export class ConfigurationManager implements IConfigurationManager {
}
getDebugger(type: string): Debugger | undefined {
return this.debuggers.filter(dbg => strings.equalsIgnoreCase(dbg.type, type)).pop();
return this.debuggers.find(dbg => strings.equalsIgnoreCase(dbg.type, type));
}
isDebuggerInterestedInLanguage(language: string): boolean {
return this.debuggers.filter(a => language && a.languages && a.languages.indexOf(language) >= 0).length > 0;
return !!this.debuggers.find(a => language && a.languages && a.languages.indexOf(language) >= 0);
}
async guessDebugger(type?: string): Promise<Debugger | undefined> {
@@ -571,7 +571,7 @@ abstract class AbstractLaunch {
return undefined;
}
return config.compounds.filter(compound => compound.name === name).pop();
return config.compounds.find(compound => compound.name === name);
}
getConfigurationNames(ignoreCompoundsAndPresentation = false): string[] {
@@ -602,7 +602,7 @@ abstract class AbstractLaunch {
return undefined;
}
return config.configurations.filter(config => config && config.name === name).shift();
return config.configurations.find(config => config && config.name === name);
}
get hidden(): boolean {
@@ -952,7 +952,7 @@ export function getStackFrameThreadAndSessionToFocus(model: IDebugModel, stackFr
session = stackFrame ? stackFrame.thread.session : thread!.session;
} else {
const sessions = model.getSessions();
const stoppedSession = sessions.filter(s => s.state === State.Stopped).shift();
const stoppedSession = sessions.find(s => s.state === State.Stopped);
session = stoppedSession || (sessions.length ? sessions[0] : undefined);
}
}
@@ -962,7 +962,7 @@ export function getStackFrameThreadAndSessionToFocus(model: IDebugModel, stackFr
thread = stackFrame.thread;
} else {
const threads = session ? session.getAllThreads() : undefined;
const stoppedThread = threads && threads.filter(t => t.stopped).shift();
const stoppedThread = threads && threads.find(t => t.stopped);
thread = stoppedThread || (threads && threads.length ? threads[0] : undefined);
}
}
@@ -890,8 +890,8 @@ export class DebugSession implements IDebugSession {
this.rawListeners.push(this.raw.onDidBreakpoint(event => {
const id = event.body && event.body.breakpoint ? event.body.breakpoint.id : undefined;
const breakpoint = this.model.getBreakpoints().filter(bp => bp.getIdFromAdapter(this.getId()) === id).pop();
const functionBreakpoint = this.model.getFunctionBreakpoints().filter(bp => bp.getIdFromAdapter(this.getId()) === id).pop();
const breakpoint = this.model.getBreakpoints().find(bp => bp.getIdFromAdapter(this.getId()) === id);
const functionBreakpoint = this.model.getFunctionBreakpoints().find(bp => bp.getIdFromAdapter(this.getId()) === id);
if (event.body.reason === 'new' && event.body.breakpoint.source && event.body.breakpoint.line) {
const source = this.getSource(event.body.breakpoint.source);
@@ -146,10 +146,10 @@ export class DebugTaskRunner {
}));
const promise: Promise<ITaskSummary | null> = this.taskService.getActiveTasks().then(async (tasks): Promise<ITaskSummary | null> => {
if (tasks.filter(t => t._id === task._id).length) {
if (tasks.find(t => t._id === task._id)) {
// Check that the task isn't busy and if it is, wait for it
const busyTasks = await this.taskService.getBusyTasks();
if (busyTasks.filter(t => t._id === task._id).length) {
if (busyTasks.find(t => t._id === task._id)) {
taskStarted = true;
return inactivePromise;
}
@@ -81,7 +81,7 @@ export class VariablesView extends ViewPane {
const scopes = await stackFrame.getScopes();
// Expand the first scope if it is not expensive and if there is no expansion state (all are collapsed)
if (scopes.every(s => this.tree.getNode(s).collapsed) && scopes.length > 0) {
const toExpand = scopes.filter(s => !s.expensive).shift();
const toExpand = scopes.find(s => !s.expensive);
if (toExpand) {
this.tree.expand(toExpand);
}
@@ -873,7 +873,7 @@ export class DebugModel implements IDebugModel {
getSession(sessionId: string | undefined, includeInactive = false): IDebugSession | undefined {
if (sessionId) {
return this.getSessions(includeInactive).filter(s => s.getId() === sessionId).pop();
return this.getSessions(includeInactive).find(s => s.getId() === sessionId);
}
return undefined;
}
@@ -924,7 +924,7 @@ export class DebugModel implements IDebugModel {
}
rawUpdate(data: IRawModelUpdate): void {
let session = this.sessions.filter(p => p.getId() === data.sessionId).pop();
let session = this.sessions.find(p => p.getId() === data.sessionId);
if (session) {
session.rawUpdate(data);
this._onDidChangeCallStack.fire(undefined);
@@ -932,7 +932,7 @@ export class DebugModel implements IDebugModel {
}
clearThreads(id: string, removeThreads: boolean, reference: number | undefined = undefined): void {
const session = this.sessions.filter(p => p.getId() === id).pop();
const session = this.sessions.find(p => p.getId() === id);
this.schedulers.forEach(scheduler => scheduler.dispose());
this.schedulers.clear();
@@ -957,8 +957,6 @@ export class DebugModel implements IDebugModel {
for (let i = 1; i < stale.length && !bottomOfCallStackChanged; i++) {
bottomOfCallStackChanged = !stale[i].equals(current[i]);
}
console.log('bottom of call stack changed');
console.log(bottomOfCallStackChanged);
if (bottomOfCallStackChanged) {
this._onDidChangeCallStack.fire();
@@ -1178,7 +1176,7 @@ export class DebugModel implements IDebugModel {
}
renameFunctionBreakpoint(id: string, name: string): void {
const functionBreakpoint = this.functionBreakpoints.filter(fbp => fbp.getId() === id).pop();
const functionBreakpoint = this.functionBreakpoints.find(fbp => fbp.getId() === id);
if (functionBreakpoint) {
functionBreakpoint.name = name;
this._onDidChangeBreakpoints.fire({ changed: [functionBreakpoint], sessionOnly: false });
@@ -1241,7 +1239,7 @@ export class DebugModel implements IDebugModel {
}
moveWatchExpression(id: string, position: number): void {
const we = this.watchExpressions.filter(we => we.getId() === id).pop();
const we = this.watchExpressions.find(we => we.getId() === id);
if (we) {
this.watchExpressions = this.watchExpressions.filter(we => we.getId() !== id);
this.watchExpressions = this.watchExpressions.slice(0, position).concat(we, this.watchExpressions.slice(position));