|
1 | 1 | import announceToScreenReader from './ScreenReaderHelper'; |
| 2 | +import p5CodeAstAnalyzer from './p5CodeAstAnalyzer'; |
| 3 | +import { getClassContext, getContext, getAST } from './renameVariableHelper'; |
2 | 4 |
|
3 | 5 | const allFuncs = require('./p5-reference-functions.json'); |
4 | 6 |
|
5 | 7 | const allFunsList = new Set(allFuncs.functions.list); |
6 | 8 |
|
7 | | -function isValidIdentifierSelection(selection, tokenType) { |
| 9 | +function isValidIdentifierSelection(cm, fromPos, selection, tokenType) { |
| 10 | + const { scopeToDeclaredVarsMap = {}, userDefinedClassMetadata = {} } = |
| 11 | + p5CodeAstAnalyzer(cm) || {}; |
| 12 | + const ast = getAST(cm); |
| 13 | + |
| 14 | + const baseContext = getContext( |
| 15 | + cm, |
| 16 | + ast, |
| 17 | + fromPos, |
| 18 | + scopeToDeclaredVarsMap, |
| 19 | + userDefinedClassMetadata |
| 20 | + ); |
| 21 | + |
| 22 | + const isInsideClassContext = baseContext in userDefinedClassMetadata; |
| 23 | + |
| 24 | + if (tokenType === 'property' && !isInsideClassContext) return false; |
8 | 25 | if (!selection || selection.trim() === '') return false; |
9 | 26 | if ( |
10 | 27 | tokenType === 'comment' || |
@@ -141,10 +158,17 @@ function openRenameInputDialog(coords, oldName, onSubmit) { |
141 | 158 | }); |
142 | 159 | } |
143 | 160 |
|
144 | | -export default function showRenameDialog(tokenType, coords, oldName, onSubmit) { |
| 161 | +export default function showRenameDialog( |
| 162 | + cm, |
| 163 | + fromPos, |
| 164 | + tokenType, |
| 165 | + coords, |
| 166 | + oldName, |
| 167 | + onSubmit |
| 168 | +) { |
145 | 169 | if ( |
146 | 170 | (allFunsList.has(oldName) && tokenType !== 'p5-variable') || |
147 | | - !isValidIdentifierSelection(oldName, tokenType) |
| 171 | + !isValidIdentifierSelection(cm, fromPos, oldName, tokenType) |
148 | 172 | ) { |
149 | 173 | showTemporaryDialog(coords, 'You cannot rename this element'); |
150 | 174 | announceToScreenReader(`The word ${oldName} cannot be renamed`, true); |
|
0 commit comments