diff --git a/src/utils/ast.js b/src/utils/ast.js index 5157dedc74..f2cab903eb 100644 --- a/src/utils/ast.js +++ b/src/utils/ast.js @@ -8,21 +8,20 @@ import { xor, range } from "lodash"; import { convertToList } from "./pause/pausePoints"; import type { Location, Source, ColumnPosition } from "../types"; +import type { Symbols } from "../reducers/ast"; -import type { - AstPosition, - AstLocation, - PausePoints, - SymbolDeclarations -} from "../workers/parser"; +import type { AstPosition, AstLocation, PausePoints } from "../workers/parser"; export function findBestMatchExpression( - symbols: SymbolDeclarations, + symbols: Symbols, tokenPos: ColumnPosition ) { - const { memberExpressions, identifiers, literals } = symbols; - const { line, column } = tokenPos; + if (symbols.loading) { + return null; + } + const { line, column } = tokenPos; + const { memberExpressions, identifiers, literals } = symbols; const members = memberExpressions.filter(({ computed }) => !computed); return [] @@ -107,18 +106,18 @@ function findClosestofSymbol(declarations: any[], location: Location) { }, null); } -export function findClosestFunction( - symbols: SymbolDeclarations, - location: Location -) { - const { functions } = symbols; - return findClosestofSymbol(functions, location); +export function findClosestFunction(symbols: ?Symbols, location: Location) { + if (!symbols || symbols.loading) { + return null; + } + + return findClosestofSymbol(symbols.functions, location); } -export function findClosestClass( - symbols: SymbolDeclarations, - location: Location -) { - const { classes } = symbols; - return findClosestofSymbol(classes, location); +export function findClosestClass(symbols: Symbols, location: Location) { + if (!symbols || symbols.loading) { + return null; + } + + return findClosestofSymbol(symbols.functions, location); } diff --git a/src/utils/function.js b/src/utils/function.js index 96f37abef2..d224a2ca5c 100644 --- a/src/utils/function.js +++ b/src/utils/function.js @@ -2,21 +2,28 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at . */ +// @flow import { findClosestFunction } from "./ast"; import { correctIndentation } from "./indentation"; +import type { Source } from "../types"; +import type { Symbols } from "../reducers/ast"; -export function findFunctionText(line, source, symbols) { +export function findFunctionText( + line: number, + source: Source, + symbols: ?Symbols +): ?string { const func = findClosestFunction(symbols, { + sourceId: source.id, line, column: Infinity }); - if (!func) { + + if (!func || !source.text) { return null; } - const { - location: { start, end } - } = func; + const { location: { start, end } } = func; const lines = source.text.split("\n"); const firstLine = lines[start.line - 1].slice(start.column); const lastLine = lines[end.line - 1].slice(0, end.column);