Skip to content

Commit

Permalink
dc: implement unused and undeclared vars fix actions
Browse files Browse the repository at this point in the history
  • Loading branch information
tom95 committed Oct 2, 2023
1 parent 685acca commit 7e8ac23
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 33 deletions.
15 changes: 11 additions & 4 deletions packages/DomainCode-Diff/DCMappingStore.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ Class {
{ #category : #'as yet unclassified' }
DCMappingStore >> addMappingFrom: src to: dest [

SBToggledCode comment: '' active: 0 do: {[src treeLabel = 'function' ifTrue: [self halt]]}.
SBToggledCode
comment: ''
active: 1
do: {[src type = 'lexical_declaration' ifTrue: [self halt]]}.
srcToDest at: src put: dest.
destToSrc at: dest put: src
]
Expand Down Expand Up @@ -96,8 +99,12 @@ DCMappingStore >> srcToDest [
DCMappingStore >> visualizeFrom: src to: dest [

| destExtent srcExtent editor |
srcExtent := src fullBounds; extent.
destExtent := dest fullBounds; extent.
srcExtent := src
fullBounds;
extent.
destExtent := dest
fullBounds;
extent.

"container := SBBlock new.
listDirection: #leftToRight; hResizing: #shrinkWrap; vResizing: #shrinkWrap; changeTableLayout; layoutInset: 10; cellGap: 50; addMorphBack: src; addMorphBack: dest."
Expand All @@ -107,5 +114,5 @@ DCMappingStore >> visualizeFrom: src to: dest [
self keysAndValuesDo: [:from :to |
from layoutInset: 10.
to layoutInset: 10.
((editor connectFrom: from to: to) color: (Color random alpha: 0.2))].
(editor connectFrom: from to: to) color: (Color random alpha: 0.5)]
]
102 changes: 96 additions & 6 deletions packages/DomainCode-Parser/DCBlock.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ DCBlock class >> registerShortcuts: aProvider [
{ #category : #smalltalk }
DCBlock class >> smalltalkBrowseImplementors [
<query: #(#shortcut)>
<query: #shortcut>
^ {
self smalltalkMessageSend.
Expand All @@ -160,7 +160,7 @@ DCBlock class >> smalltalkBrowseImplementors [
{ #category : #smalltalk }
DCBlock class >> smalltalkBrowseReferencesSend [
<query: #(#shortcut)>
<query: #shortcut>
^ {
self smalltalkMessageSend.
Expand Down Expand Up @@ -210,14 +210,13 @@ DCBlock class >> smalltalkDeclaration [
{ #category : #smalltalk }
DCBlock class >> smalltalkHighlightIdentifiers [
<query: #(selection)>
<query: #selection>
^ {
[:x | x isSelected].
self smalltalkDeclaration.
self smalltalkUsesOfDeclaration.
[:decl :identifiers | identifiers do: #attachHighlight]
}
[:decl :identifiers | identifiers do: #attachHighlight]}
]
{ #category : #smalltalk }
Expand All @@ -240,6 +239,60 @@ DCBlock class >> smalltalkMessageSendSelector [
[:parts :message | {#args. parts joinSeparatedBy: ''. message}]}
]
{ #category : #smalltalk }
DCBlock class >> smalltalkUndeclaredVariable [
<query: #change>
^ {
[:x | x language = SBTSSmalltalk].
[:x | x type = #identifier].
[:x |
(DCQuery script: self smalltalkDeclaration with: x)
ifNil: [x]
ifNotNil: [nil]].
[:x |
x reportError: (SBErrorDecorator new
message: 'undeclared variable';
fixActions: {
SBCodeAction labeled: 'Declare block-local' for: x do: [:node | | block decl |
decl := DCBlock new
type: #identifier;
addMorphBack: (DCText new contents: node contents).
block := node orAnyParent: {#block. #method}.
block childSandblocks
detect: [:b | b type = #temporaries]
ifFound: [:temporaries |
node sandblockEditor do: (SBInsertCommand new
morph: decl;
container: temporaries;
index: temporaries submorphCount)]
ifNone: [ | index |
index := block submorphs findFirst: [:s | s treeLabel = '|'].
node sandblockEditor do: (SBInsertCommand new
morph: (DCBlock new
type: #temporaries;
addMorphBack: (DCText new contents: '|');
addMorphBack: decl;
addMorphBack: (DCText new contents: '|'));
index: (index = 0 ifTrue: [2] ifFalse: [index + 1]);
container: block)]]})]}
]
{ #category : #smalltalk }
DCBlock class >> smalltalkUnusedVariable [
<query: #change>
^ {
[:x | x language = SBTSSmalltalk].
[:x | x hasParent: #temporaries].
self smalltalkUsesOfDeclaration.
[:decl :uses | uses size = 1].
[:decl :uses |
decl reportError: (SBErrorDecorator new
message: 'unused variable';
fixActions: {SBCodeAction labeled: 'Remove unused declaration' for: decl do: [:node | node deleteBlock]})]}
]
{ #category : #smalltalk }
DCBlock class >> smalltalkUsesOfDeclaration [
Expand Down Expand Up @@ -315,6 +368,12 @@ DCBlock >> allTextMorphsDo: aBlock [
self allMorphsDo: [:m | m isTextMorph ifTrue: [aBlock value: m]]
]
{ #category : #'as yet unclassified' }
DCBlock >> artefactChanged: aMethodBlock [
self = aMethodBlock ifTrue: [self queryState updateChangeQueries]
]
{ #category : #'query actions' }
DCBlock >> attachHighlight [
Expand Down Expand Up @@ -505,6 +564,18 @@ DCBlock >> handleInsertEvent: anEvent in: textMorph [
^ super keyStroke: anEvent
]
{ #category : #queries }
DCBlock >> hasParent: aCollectionOrSymbol [
| matches |
self isRootBlock ifTrue: [^ false].
matches := aCollectionOrSymbol isSymbol
ifTrue: [{aCollectionOrSymbol}]
ifFalse: [aCollectionOrSymbol].
^ matches includes: self owner type
]
{ #category : #'as yet unclassified' }
DCBlock >> highlight [
Expand Down Expand Up @@ -631,6 +702,12 @@ DCBlock >> intoWorld: aWorld [
to: self queryState]
]
{ #category : #'as yet unclassified' }
DCBlock >> isArtefact [
^ self isRootBlock
]
{ #category : #'as yet unclassified' }
DCBlock >> isBlockBody [
Expand Down Expand Up @@ -688,7 +765,7 @@ DCBlock >> keyStroke: anEvent [
{ #category : #'as yet unclassified' }
DCBlock >> language [
^ language
^ language ifNil: [self owner language]
]
{ #category : #'as yet unclassified' }
Expand Down Expand Up @@ -924,12 +1001,25 @@ DCBlock >> replace: oldTree with: newTree [
yourself]}
]
{ #category : #'query actions' }
DCBlock >> reportError: anError [
self queryState reportError: anError for: self
]
{ #category : #'as yet unclassified' }
DCBlock >> rootBlock [
^ self orOwnerSuchThat: [:o | o isTSBlock and: [o type = self language rootRuleName]]
]
{ #category : #'as yet unclassified' }
DCBlock >> saveTryFixing: aFixBoolean quick: aQuickBoolean [
self queryState errors ifNotEmpty: [:errors | aFixBoolean ifTrue: [errors do: [:error | error tryFixIfFail: [^ false]]] ifFalse: [^ false]].
^ true
]
{ #category : #'as yet unclassified' }
DCBlock >> shallowCopyBlock [
Expand Down
77 changes: 54 additions & 23 deletions packages/DomainCode-Parser/DCQueryState.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,40 @@ Class {
#name : #DCQueryState,
#superclass : #Object,
#instVars : [
'highlights',
'newHighlights',
'currentDecorators',
'newDecorators',
'rootBlock',
'selectionUpdateQueued',
'currentShortcut'
'currentShortcut',
'updateQueued'
],
#category : #'DomainCode-Parser'
}

{ #category : #'as yet unclassified' }
DCQueryState >> errors [

^ Array streamContents: [:s | self rootBlock allBlocksDo: [:b | b withDecorator: SBErrorDecorator do: [:d | s nextPut: d] ifAbsent: []]]
]

{ #category : #'as yet unclassified' }
DCQueryState >> highlight: aBlock [

newHighlights add: aBlock
(newDecorators at: aBlock ifAbsentPut: [OrderedCollection new]) add: SBHighlightDecorator new
]

{ #category : #'as yet unclassified' }
DCQueryState >> initialize [

super initialize.

highlights := WeakSet new.
selectionUpdateQueued := false
currentDecorators := Dictionary new
at: #selection put: WeakKeyDictionary new;
at: #change put: WeakKeyDictionary new;
yourself.
updateQueued := Dictionary new
]

{ #category : #'as yet unclassified' }
{ #category : #shortcuts }
DCQueryState >> performShortcut: aSymbol for: aBlock [

[
Expand All @@ -42,32 +51,42 @@ DCQueryState >> queriesFor: aSymbol do: aBlock [

Pragma
withPragmasIn: DCBlock class
do: [:pragma | (pragma keyword = #query: and: [pragma arguments first includes: aSymbol]) ifTrue: [aBlock value: pragma selector]]
do: [:pragma | (pragma keyword = #query: and: [pragma arguments first = aSymbol]) ifTrue: [aBlock value: pragma selector]]
]

{ #category : #'as yet unclassified' }
DCQueryState >> rerunQueriesDo: aBlock [
DCQueryState >> reportError: aDecorator for: aBlock [

newHighlights := WeakSet new.
aBlock value.
highlights do: [:block | (newHighlights includes: block) ifFalse: [block detachDecorators: SBHighlightDecorator]].
newHighlights do: [:block | (highlights includes: block) ifFalse: [block attachDecorator: SBHighlightDecorator new]].
highlights := newHighlights
(newDecorators at: aBlock ifAbsentPut: [OrderedCollection new]) add: aDecorator
]

{ #category : #'as yet unclassified' }
DCQueryState >> rerunQueriesFor: aSymbol do: aBlock [

| oldDecorators |
newDecorators := WeakKeyDictionary new.

aBlock value.

oldDecorators := currentDecorators at: aSymbol.
oldDecorators do: [:list | list do: [:d | d detach]].
newDecorators keysAndValuesDo: [:block :list | list do: [:d | block attachDecorator: d]].
currentDecorators at: aSymbol put: newDecorators
]

{ #category : #accessing }
DCQueryState >> rootBlock [

^ rootBlock
]

{ #category : #'as yet unclassified' }
{ #category : #accessing }
DCQueryState >> rootBlock: aBlock [

rootBlock := aBlock
]

{ #category : #'as yet unclassified' }
{ #category : #shortcuts }
DCQueryState >> tryShortcut: aSymbol do: aBlock [

currentShortcut = aSymbol ifTrue: [
Expand All @@ -76,14 +95,26 @@ DCQueryState >> tryShortcut: aSymbol do: aBlock [
]

{ #category : #'as yet unclassified' }
DCQueryState >> updateSelectionQueries [
DCQueryState >> updateChangeQueries [

self updateQueriesFor: #change
]

{ #category : #'as yet unclassified' }
DCQueryState >> updateQueriesFor: aSymbol [

selectionUpdateQueued ifTrue: [^ self].
selectionUpdateQueued := true.
(updateQueued at: aSymbol ifAbsent: [false]) ifTrue: [^ self].
updateQueued at: aSymbol put: true.
Project current addDeferredUIMessage: [
selectionUpdateQueued := false.
self rerunQueriesDo: [
updateQueued at: aSymbol put: false.
self rerunQueriesFor: aSymbol do: [
self
queriesFor: #selection
queriesFor: aSymbol
do: [:selector | self rootBlock allBlocksDo: [:b | DCQuery script: (DCBlock perform: selector) with: b]]]]
]

{ #category : #'as yet unclassified' }
DCQueryState >> updateSelectionQueries [

self updateQueriesFor: #selection
]

0 comments on commit 7e8ac23

Please sign in to comment.