'From Cuis 2.0 of 24 February 2010 [latest update: #440] on 10 March 2010 at 11:08:10 am'!

!SHTextStylerST80 methodsFor: 'private' stamp: 'jmv 3/10/2010 11:00'!
replaceStringForRangesWithType: aSymbol with: aString in: aText 
	"Answer aText if no replacements, or a copy of aText with 
	each range with a type of aSymbol replaced by aString"
	| answer toReplace adjustSourceMap increaseInLength |

	"jmvNote, 10-March-2010
	#sourceMap used to be a collection. Now it is a kind of DebuggerMethodMap.
	While we could adjust the proper ivars in the sourceMap as it was done when it was simply a collection,
	a much simpler and acceptable approach is to simply disable string replacement when in a debugger.
	This actually makes the sourceMap ivar almost useless and the processedSourceMap ivar completely useless.
	I leave them, and avoid modifying their references just in case we might want to enable string replacing in debugger again."
	sourceMap ifNotNil: [^aText].
	
	toReplace _ (self rangesIn: aText setWorkspace: false) 
		select: [:each | each type = aSymbol].
	toReplace isEmpty ifTrue: [^aText].
	answer _ aText copy.
	increaseInLength _ 0.
	adjustSourceMap _ sourceMap notNil and:[sourceMap ~~ processedSourceMap].

	(toReplace asSortedCollection: [:a :b | a start <= b start]) 
		do: [:each | | end start thisIncrease | 
			start _ each start + increaseInLength.
			end _ each end + increaseInLength.
			answer 	replaceFrom: start to: end with: aString.
			thisIncrease _ aString size - each length.
			increaseInLength _ increaseInLength + thisIncrease.
			adjustSourceMap ifTrue:[
				sourceMap do:[:assoc | | first newFirst last newLast |
					first _ newFirst _ assoc value first.
					last _ newLast _ assoc value last.
					first > start ifTrue:[newFirst _ first + thisIncrease].
					last > start ifTrue:[newLast _ last + thisIncrease].
					(first ~= newFirst or:[last ~= newLast])
						ifTrue:[assoc value: (newFirst to: newLast)]]]].
	adjustSourceMap ifTrue:[processedSourceMap _ sourceMap]. 
	^answer! !

