Skip to content

Commit

Permalink
Fixed evaluate autocomplete.
Browse files Browse the repository at this point in the history
  • Loading branch information
amyjko committed Sep 28, 2024
1 parent 16557a7 commit 653abfd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Dates are in `YYYY-MM-DD` format and versions are in [semantic versioning](http:
- Consistent rendering of inferred and explicit placeholder types.
- Better type checking on operator wrapping
- Don't show full names of operators.
- Fixed evaluate autocomplete.

## 0.12.0 2024-09-22

Expand Down
31 changes: 16 additions & 15 deletions src/edit/Caret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ import getPreferredSpaces from '@parser/getPreferredSpaces';
import { UnknownName } from '@conflicts/UnknownName';
import BinaryEvaluate from '@nodes/BinaryEvaluate';
import Evaluate from '@nodes/Evaluate';
import FunctionDefinition from '@nodes/FunctionDefinition';
import StructureDefinition from '@nodes/StructureDefinition';
import StructureType from '@nodes/StructureType';
import FunctionType from '@nodes/FunctionType';
import PropertyReference from '@nodes/PropertyReference';

export type InsertionContext = { before: Node[]; after: Node[] };
export type CaretPosition = number | Node;
Expand Down Expand Up @@ -1002,9 +1003,9 @@ export default class Caret {
// Did we somehow get no source?
if (newSource === undefined) return undefined;

// After the autcomplete, are we no longer inserting text, as indicated by a node position?
// After the autcomplete, are we no longer inserting text, as indicated by empty text insertion?
// Return what autocomplete returned.
if (typeof newPosition !== 'number')
if (text === '' || typeof newPosition !== 'number')
return [
newSource,
this.withSource(newSource).withPosition(newPosition),
Expand Down Expand Up @@ -1126,23 +1127,23 @@ export default class Caret {
const precedingExpression = this.source
.nodes()
.filter(
(node): node is Reference =>
node instanceof Reference &&
(node): node is Expression =>
(node instanceof Reference ||
node instanceof PropertyReference ||
(node instanceof Block && !node.isRoot())) &&
source.getNodeLastPosition(node) === position,
)[0];
)
.at(-1);

if (precedingExpression) {
const context = project.getNodeContext(precedingExpression);
const fun = precedingExpression.resolve(context);
const fun = precedingExpression.getType(context);
if (
fun instanceof FunctionDefinition ||
fun instanceof StructureDefinition
(fun instanceof FunctionType ||
fun instanceof StructureType) &&
fun.definition !== undefined
) {
const evaluate = fun.getEvaluateTemplate(
project.basis.locales,
context,
undefined,
);
const evaluate = Evaluate.make(precedingExpression, []);
// Make a new source
newSource = source.replace(precedingExpression, evaluate);
// Place the caret on the placeholder
Expand Down

0 comments on commit 653abfd

Please sign in to comment.