|
227 | 227 | * (where Arranger will already have been registered if instantiated). |
228 | 228 | * Kay Gürtzig 2024-11-27 Bugfix #1181: Execution highlighting in the code preview was compromised |
229 | 229 | * after Calls and within multi-line Calls |
| 230 | + * Kay Gürtzig 2025-01-21 Enh. #1184: Lazy multi-dimensional array creation on first element assignment |
230 | 231 | * |
231 | 232 | ****************************************************************************************************** |
232 | 233 | * |
@@ -4389,16 +4390,27 @@ else if (isConstant && content instanceof HashMap<?,?>) { |
4389 | 4390 | // Adjust the basic level - find the variable or object or create it |
4390 | 4391 | int level = 0; |
4391 | 4392 | boolean arrayWanted = false; |
4392 | | - if ((targetObject == null) && accessPath.count() < 3) { |
| 4393 | + // START KGU#1169 2025-01-17: Issue #1184 Allow creation of multidimensional arrays |
| 4394 | + //if ((targetObject == null) && accessPath.count() < 3) { |
| 4395 | + int nPathItems = accessPath.count(); |
| 4396 | + boolean onlyIndices = accessPath.get(1).startsWith("["); |
| 4397 | + for (int i = 2; onlyIndices && i < nPathItems; i++) { |
| 4398 | + onlyIndices = accessPath.get(i).startsWith("["); |
| 4399 | + } |
| 4400 | + if ((targetObject == null) && (onlyIndices || nPathItems < 3)) { |
| 4401 | + // END KGU#1169 2025-01-17 |
4393 | 4402 | // We may generously create it, in particular if the type is declared |
4394 | 4403 | String access = accessPath.get(1); |
4395 | 4404 | if ((arrayWanted = access.startsWith("[")) |
4396 | 4405 | && (targetType == null || targetType.isArray())) { |
4397 | | - int index = Integer.parseInt(access = access.substring(1)); |
4398 | | - targetObject = objectArray = new ArrayList<Object>(index+1); |
4399 | | - for (int i = 0; i <= index; i++) { |
4400 | | - objectArray.add(0); |
4401 | | - } |
| 4406 | + // START KGU#1169 2025-01-21: Issue #1184 |
| 4407 | + //int index = Integer.parseInt(access = access.substring(1)); |
| 4408 | + //targetObject = objectArray = new ArrayList<Object>(index+1); |
| 4409 | + //for (int i = 0; i <= index; i++) { |
| 4410 | + // objectArray.add(0); |
| 4411 | + //} |
| 4412 | + targetObject = objectArray = createArrayForIndexList(accessPath.subSequence(1, nPathItems), true); |
| 4413 | + // END KGU#1169 2025-01-21 |
4402 | 4414 | if (typeStr == null) { |
4403 | 4415 | typeStr = "@???"; |
4404 | 4416 | } |
@@ -4685,6 +4697,37 @@ else if (compObject != null){ |
4685 | 4697 | return target; // Base name of the assigned variable or constant |
4686 | 4698 | // END KGU#580 2018-09-24 |
4687 | 4699 | } |
| 4700 | + |
| 4701 | + // START KGU#1169 2025-01-21: Issue #1184 |
| 4702 | + /** |
| 4703 | + * Recursive routine to construct a mult-dimensional array structure containing |
| 4704 | + * at least the index path symbolized by {@code indexPath}. |
| 4705 | + * |
| 4706 | + * @param indexPath - a StringList expected to contain strings of form {@code "[<int>"} |
| 4707 | + * where {@code <int>} is meant to be an unsigned integer literal |
| 4708 | + * @param fill TODO |
| 4709 | + * @return An ArrayList containing as many elements as the integer literal in |
| 4710 | + * the first element of {@code indexPath}. If {@code indexPath} has length |
| 4711 | + * 1 then the ArrayList element will be 0, otherwise they will be ArrayLists |
| 4712 | + * themselves |
| 4713 | + */ |
| 4714 | + private ArrayList<Object> createArrayForIndexList(StringList indexPath, boolean fill) |
| 4715 | + { |
| 4716 | + String access = indexPath.get(0); |
| 4717 | + boolean interLevel = indexPath.count() > 1; |
| 4718 | + int index = Integer.parseInt(access = access.substring(1)); |
| 4719 | + ArrayList<Object> targetArray = new ArrayList<Object>(index+1); |
| 4720 | + for (int i = 0; i <= index; i++) { |
| 4721 | + if (interLevel) { |
| 4722 | + targetArray.add(createArrayForIndexList(indexPath.subSequence(1, indexPath.count()), fill && i == index)); |
| 4723 | + } |
| 4724 | + else if (fill) { |
| 4725 | + targetArray.add(0); |
| 4726 | + } |
| 4727 | + } |
| 4728 | + return targetArray; |
| 4729 | + } |
| 4730 | + // END KGU#1169 2025-01-21 |
4688 | 4731 |
|
4689 | 4732 | /** |
4690 | 4733 | * Checks compatibility of types between {@code target} (i.e. {@code targetType} |
|
0 commit comments