Debugger >> initializeFull
initializeFull
	"Expand the stack for the full debugger. Create inspectors."
	
	| oldIndex |
	oldIndex := self contextStackIndex.
	contextStackIndex := 0.
	
	self expandStack.

	receiverInspector := Inspector on: nil.
	contextVariablesInspector := ContextVariablesInspector on: nil.
	
	self toggleContextStackIndex: oldIndex.

Debugger >> buildWithLabel:contents:fullView:
buildWithLabel: title contents: contentsStringOrNil fullView: full 

	| builder |
	builder := ToolBuilder default.
	^ full
		ifTrue: [
			self
				initializeFull;
				buildFullWith: builder label: title]
		ifFalse: [
			self
				expandNotifierStack;
				buildNotifierWith: builder label: title message: contentsStringOrNil]

Debugger >> buildControlButtonsWith:
buildControlButtonsWith: builder

	^ self buildControlButtonsWith: builder specs:
		(self customButtonSpecs select: [:spec |
			spec
				at: 5 ifPresent: [:visibilitySelector | self perform: visibilitySelector]
				ifAbsent: [true]])

Debugger >> customButtonSpecs
customButtonSpecs
	"Answer an array of elements of the form wording, selector, help-message, that characterize the custom button row of a debugger."

	| list |
	list := #(('Proceed'	proceed				'Close the debugger and proceed.'	interruptedProcessShouldResume)
		('Restart'		restart				'Reset this context to its start.')
		('Into'			stepInto				'step Into message sends'	interruptedProcessIsReady)
		('Over'			stepOver				'step Over message sends'	interruptedProcessIsReady)
		('Through'		stepThrough		'step into a block'			interruptedProcessIsReady)
		('Simulate'		simulateStep		'simulate bytecode step'			interruptedProcessIsReady	showingByteCodes)
		('Full Stack'		showFullStack			'show full stack')
		('Where'		showWhere				'select current pc range')
		('Tally It'			tally				'evaluate current selection and measure the time')).
	(Preferences restartAlsoProceeds and: [self interruptedProcessShouldResume]) ifTrue:
		[list := list collect: [:each |
			each second == #restart
				ifTrue: [each copy
						at: 1 put: 'Proceed Here';
						at: 3 put: 'Proceed from the beginning of this context.';
						yourself]
				ifFalse: [each second == #proceed
					ifTrue: [each copy
							at: 1 put: 'Proceed Top';
							at: 3 put: 'Proceed from the current top context.';
							yourself]
					ifFalse: [each]]]].
	^ list

Debugger >> send
send
	"Send the selected message in the accessed method, and take control in 
	the method invoked to allow further step or send. Aka stepInto."

	| newContext currentContext |
	self okToChange ifFalse: [^ self].
	self interruptedProcessIsReady ifFalse: [^ self shouldNotStep].
	
	self checkContextSelection.
	currentContext := self selectedContext.
	newContext := self basicSend.
	
	self updateProcess.
	self contextStackIndex > 1
		ifTrue: [self resetContext: newContext]
		ifFalse:
			[newContext == currentContext
				ifTrue: [self changed: #contentsSelection.
						self updateInspectors]
				ifFalse: [self resetContext: newContext]]

Debugger >> doStep
doStep
	"Send the selected message in the accessed method, and regain control 
	after the invoked method returns. Aka stepOver."
	
	| currentContext newContext |
	self okToChange ifFalse: [^ self].
	self interruptedProcessIsReady ifFalse: [^ self shouldNotStep].
	
	self checkContextSelection.
	currentContext := self selectedContext.
	newContext := self basicDoStep.
	
	self updateProcess.
	self contextStackIndex > 1
		ifTrue: [self resetContext: newContext]
		ifFalse:
			[newContext == currentContext
				ifTrue: [self changed: #contentsSelection.
						self updateInspectors]
				ifFalse: [self resetContext: newContext]].

Debugger >> resetContext:
resetContext: aContext 
	^ self resetContext: aContext changeContents: true

Debugger >> initialize
initialize

	super initialize.

	selectingPC := true.
	contextStackIndex := 0.
	
	"The default termination procedure is aggressive to ignore currently running, and thus erroneous, ensure-blocks in the debugged process. The preference can change that."
	terminateProcessSelector := self class closeMeansAbandon
		ifTrue: [#terminateAggressively]
		ifFalse:[#terminate].

Debugger >> tryRestartFrom:
tryRestartFrom: context
	"Try to restart from the initial state of the context.
	Return whether an unwind error occurred."

	| currentContext unwindError |
	currentContext := self selectedContext.
	self
		handleLabelUpdatesIn: [currentContext := interruptedProcess popTo: context]
		whenExecuting: currentContext.
	unwindError := currentContext ~= context.
	unwindError ifFalse: [
		self
			handleLabelUpdatesIn:
				[interruptedProcess restartTop.
				 currentContext := self stepToStatement]
			whenExecuting: currentContext].
	self resetContext: currentContext.
	^ unwindError

Debugger >> resumeProcess:
resumeProcess: aProcess
	"Let the current project take care of resuming aProcess. The receiver's window (or topView) was probably closed already. Complements #openWithLabel:contents:fullView:. See #proceed."

	Project current resumeProcessSafely: aProcess.

ToolBuilder class >> build:
build: aClass
	^self default build: aClass

ToolBuilder class >> open:
open: aClass
	^self default open: aClass

Context >> tempNames
tempNames
	"Answer a SequenceableCollection of the names of the receiver's temporary 
	 variables, which are strings."

	^ self debuggerMap tempNamesForContext: self

Process class >> forBlock:runUntil:
forBlock: aBlock runUntil: aConditionBlock
	"Create a process for the given block. Simulate code execution until the provided condition is fulfilled."

	^ aBlock newProcess
		runUntil: aConditionBlock;
		yourself

Debugger >> newStack:
newStack: stack
	| oldStack diff matchIndex |
	oldStack := contextStack.
	contextStack := stack.
	(oldStack isNil or: [oldStack last ~~ stack last or: [oldStack first ~~ stack first]]) ifTrue:
		[contextStackList := stack collect: [:ctx | ctx printString].
		 ^self].
	"May be able to re-use some of previous list"
	diff := stack size - oldStack size.
	contextStackList := diff <= 0
		ifTrue: [contextStackList copyFrom: 1 - diff to: oldStack size]
		ifFalse:
			[matchIndex := stack lastIndexOf: oldStack first startingAt: diff + 1.
			 matchIndex = 0
				ifTrue: [contextStack collect: [:ctx | ctx printString]]
				ifFalse: [((contextStack first: matchIndex - 1) collect: [:ctx| ctx printString]), contextStackList]]
						"#(d e f h i) => #(a b c d e f g h i)
						diff := 3.
						matchIndex := 4.
						#(a b c d e f g h i) := #(a b c), #(d e f h i)"

Debugger >> contextStackList
contextStackList
	"Answer the array of contexts."

	^contextStackList

Debugger >> debuggerCodePaneMenu:
debuggerCodePaneMenu: aMenu
	<codePaneMenu>
	<menuPriority: 200>
	^ aMenu
		add: 'run to here' target: self selector: #runToSelection: argument: self codePaneSelectionInterval;
			balloonTextForLastItem: 'Step through until the selected expression is reached' translated;
		add: 'run until...' target: self selector: #runUntil;
			balloonTextForLastItem: 'Step over until a custom condition is matched' translated;
		add: 'send until...' target: self selector: #sendUntil;
			balloonTextForLastItem: 'Step into until a custom condition is matched' translated;
		addLine;
		yourself


