From 7daf2685905d11a0107533c5a9fa55e0bd860b7e Mon Sep 17 00:00:00 2001 From: Tom Beckmann Date: Thu, 28 Sep 2023 15:52:34 +0200 Subject: [PATCH] dc: skip closing pairs --- packages/DomainCode-Parser/DCBlock.class.st | 38 +++++++++++++++---- .../DCPairCompleteTest.class.st | 13 ++++++- .../SBTSPosition.class.st | 5 ++- .../Sandblocks-TreeSitter/SBTSRange.class.st | 6 +++ 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/packages/DomainCode-Parser/DCBlock.class.st b/packages/DomainCode-Parser/DCBlock.class.st index 44f63a0..bb2feb0 100644 --- a/packages/DomainCode-Parser/DCBlock.class.st +++ b/packages/DomainCode-Parser/DCBlock.class.st @@ -94,7 +94,7 @@ DCBlock class >> parse: aString old: oldBlock edit: anEdit language: aLanguage [ toValue: aString; yourself. - {block. changedRanges}] + {block. changedRanges. self findChangeRangeFrom: (oldBlock valueOfProperty: #tsSource) to: aString}] ] { #category : #'as yet unclassified' } @@ -226,6 +226,23 @@ DCBlock >> field: aString [ field := aString ] +{ #category : #'as yet unclassified' } +DCBlock >> findLastMatchingAncesors: aCollection oldTree: aBlock [ + + | oldCurrent | + oldCurrent := aBlock. + aCollection allButLast reverseDo: [:newCurrent | + oldCurrent := oldCurrent submorphs at: newCurrent submorphIndex ifAbsent: [ + self assert: oldCurrent type = newCurrent owner type. + ^ {oldCurrent. newCurrent owner}]. + oldCurrent type = newCurrent type ifFalse: [ + self assert: oldCurrent owner type = newCurrent owner type. + ^ {oldCurrent owner. newCurrent owner}]]. + + self assert: aCollection first type = oldCurrent type. + ^ {aCollection first. oldCurrent} +] + { #category : #'as yet unclassified' } DCBlock >> firstSourceCharacter [ @@ -435,6 +452,9 @@ DCBlock >> keyStroke: anEvent [ at: cursorIndex + 1 do: [:new :edit | apply value: new value: edit value: cursorIndex + 1]]. (input first isPrintable and: [anEvent commandKeyPressed not]) ifTrue: [ + "do not place closing pair characters if they are coming up right after" + (self pairMap keyAtValue: input ifAbsent: nil) ifNotNil: [:openChar | (source at: cursorIndex + 1 ifPresent: [:char | input first = char] ifAbsent: [false]) ifTrue: [^ self owner startInputAtSourceIndex: cursorIndex + 1]]. + self pairMap at: input ifPresent: [:complete | "do not autocomplete quotes in words" (complete = '''' and: [source at: cursorIndex ifPresent: #isAlphaNumeric ifAbsent: [false]]) ifTrue: [^ insert value: input]. @@ -504,7 +524,7 @@ DCBlock >> ownerWithForegroundColor [ { #category : #'as yet unclassified' } DCBlock >> pairMap [ - ^ Dictionary newFrom: {'(' -> ')'. '''' -> ''''. '"' -> '"'. '`' -> '`'. '{' -> '}'. '<' -> '>'. '[' -> ']'} + ^ Dictionary newFrom: {'(' -> ')'. '''' -> ''''. '"' -> '"'. '`' -> '`'. '{' -> '}'. '[' -> ']'} ] { #category : #'as yet unclassified' } @@ -602,7 +622,7 @@ DCBlock >> smallestBlockEncompassig: aRange [ | min | min := self. - self allBlocksDo: [:block | (block encompasses: aRange) ifTrue: [min recursiveSubmorphCount > block recursiveSubmorphCount ifTrue: [min := block]]]. + self allBlocksDo: [:block | (block encompasses: aRange) ifTrue: [min range size > block range size ifTrue: [min := block]]]. ^ min ] @@ -746,12 +766,14 @@ DCBlock >> tryApplyChange: aClosure [ value: (self activeTextMorph ifNotNil: [self activeTextMorph range start index + (oldCursorOffset - 1)]) value: [:newSource :edit :newIndex | newTree := DCBlock parse: newSource old: oldTree edit: edit language: self language. - self replace: oldTree with: newTree first. - self placeCursorIn: oldTree at: newIndex. - newTree second ifNotEmpty: [:changedRanges | | contained | - contained := oldTree smallestBlockEncompassig: (SBTSRange merging: changedRanges). - contained attachDecorator: SBFlashDecorator new]] + newTree second ifNotEmpty: [:changedRanges | | change newContained | + change := SBTSRange merging: changedRanges. + newContained := newTree first smallestBlockEncompassig: change. + self findLastMatchingAncesors: {newContained}, newContained allParents oldTree: oldTree]. + + self replace: oldTree with: newTree first. + self placeCursorIn: oldTree at: newIndex] ] { #category : #'as yet unclassified' } diff --git a/packages/DomainCode-Parser/DCPairCompleteTest.class.st b/packages/DomainCode-Parser/DCPairCompleteTest.class.st index 12fc41d..6f8597f 100644 --- a/packages/DomainCode-Parser/DCPairCompleteTest.class.st +++ b/packages/DomainCode-Parser/DCPairCompleteTest.class.st @@ -7,7 +7,7 @@ Class { { #category : #'as yet unclassified' } DCPairCompleteTest >> expectedFailures [ - ^ #(#testDeleteInPair #testJumpAfterClosedPair #testSkipClosedBracket) + ^ #(#testDeleteInPair #testJumpAfterClosedPair) ] { #category : #'as yet unclassified' } @@ -118,3 +118,14 @@ DCPairCompleteTest >> testSkipInsertQuoteWhenInWord [ self type: '''' in: editor. self assert: 'a''' equals: editor childSandblocks first sourceString ] + +{ #category : #'as yet unclassified' } +DCPairCompleteTest >> testSkipStringInString [ + + | program editor | + program := DCBlock parse: 'a' language: SBJavascript. + editor := self editorAndWorldFor: program. + program lastDeepChild startInputAtEnd. + self type: '("a"' in: editor. + self assert: 'a("a")' equals: editor childSandblocks first sourceString +] diff --git a/packages/Sandblocks-TreeSitter/SBTSPosition.class.st b/packages/Sandblocks-TreeSitter/SBTSPosition.class.st index 31d2824..0fbb095 100644 --- a/packages/Sandblocks-TreeSitter/SBTSPosition.class.st +++ b/packages/Sandblocks-TreeSitter/SBTSPosition.class.st @@ -47,7 +47,10 @@ SBTSPosition class >> null [ SBTSPosition >> + aNumber [ "advance the character index, does not consider line breaks" - ^ self copy line: line character: character + aNumber index: index + aNumber + ^ self copy + line: line + character: (character ifNotNil: [character + aNumber]) + index: index + aNumber ] { #category : #converting } diff --git a/packages/Sandblocks-TreeSitter/SBTSRange.class.st b/packages/Sandblocks-TreeSitter/SBTSRange.class.st index 3bc06e3..ff81cf6 100644 --- a/packages/Sandblocks-TreeSitter/SBTSRange.class.st +++ b/packages/Sandblocks-TreeSitter/SBTSRange.class.st @@ -53,6 +53,12 @@ SBTSRange class >> start: aNumber size: anotherNumber [ end: (SBTSPosition line: nil character: nil index: aNumber + anotherNumber) ] +{ #category : #converting } +SBTSRange >> + aDelta [ + + ^ self class start: self start + aDelta end: self end + aDelta +] + { #category : #comparing } SBTSRange >> = anObject [