Skip to content

Commit

Permalink
dc: skip closing pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
tom95 committed Sep 28, 2023
1 parent 04d0545 commit 7daf268
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
38 changes: 30 additions & 8 deletions packages/DomainCode-Parser/DCBlock.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down Expand Up @@ -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 [

Expand Down Expand Up @@ -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].
Expand Down Expand Up @@ -504,7 +524,7 @@ DCBlock >> ownerWithForegroundColor [
{ #category : #'as yet unclassified' }
DCBlock >> pairMap [

^ Dictionary newFrom: {'(' -> ')'. '''' -> ''''. '"' -> '"'. '`' -> '`'. '{' -> '}'. '<' -> '>'. '[' -> ']'}
^ Dictionary newFrom: {'(' -> ')'. '''' -> ''''. '"' -> '"'. '`' -> '`'. '{' -> '}'. '[' -> ']'}
]

{ #category : #'as yet unclassified' }
Expand Down Expand Up @@ -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
]

Expand Down Expand Up @@ -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' }
Expand Down
13 changes: 12 additions & 1 deletion packages/DomainCode-Parser/DCPairCompleteTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Class {
{ #category : #'as yet unclassified' }
DCPairCompleteTest >> expectedFailures [

^ #(#testDeleteInPair #testJumpAfterClosedPair #testSkipClosedBracket)
^ #(#testDeleteInPair #testJumpAfterClosedPair)
]

{ #category : #'as yet unclassified' }
Expand Down Expand Up @@ -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
]
5 changes: 4 additions & 1 deletion packages/Sandblocks-TreeSitter/SBTSPosition.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
6 changes: 6 additions & 0 deletions packages/Sandblocks-TreeSitter/SBTSRange.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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 [

Expand Down

0 comments on commit 7daf268

Please sign in to comment.