Fix formatting for body sources missing newlines

pull/10616/head
Andrew Watkins 2018-04-25 10:45:56 -07:00
parent b14a59dbe2
commit bfdce4594f
4 changed files with 45 additions and 25 deletions

View File

@ -21,6 +21,8 @@ class BodyBuilder extends PureComponent<Props> {
return b.declarations.map(d => { return b.declarations.map(d => {
if (d.funcs) { if (d.funcs) {
return ( return (
<div key={b.id}>
<div className="func-node--name">{d.name} =</div>
<ExpressionNode <ExpressionNode
key={b.id} key={b.id}
bodyID={b.id} bodyID={b.id}
@ -28,10 +30,15 @@ class BodyBuilder extends PureComponent<Props> {
funcNames={this.funcNames} funcNames={this.funcNames}
funcs={d.funcs} funcs={d.funcs}
/> />
</div>
) )
} }
return <div key={b.id}>{b.source}</div> return (
<div className="func-node--name" key={b.id}>
{b.source}
</div>
)
}) })
} }

View File

@ -22,14 +22,6 @@ class ExpressionNode extends PureComponent<Props> {
{({onDeleteFuncNode, onAddNode, onChangeArg, onGenerateScript}) => { {({onDeleteFuncNode, onAddNode, onChangeArg, onGenerateScript}) => {
return ( return (
<div className="func-nodes-container"> <div className="func-nodes-container">
<h4>
<FuncSelector
bodyID={bodyID}
funcs={funcNames}
onAddNode={onAddNode}
declarationID={declarationID}
/>
</h4>
{funcs.map(func => ( {funcs.map(func => (
<FuncNode <FuncNode
key={func.id} key={func.id}
@ -41,6 +33,12 @@ class ExpressionNode extends PureComponent<Props> {
onGenerateScript={onGenerateScript} onGenerateScript={onGenerateScript}
/> />
))} ))}
<FuncSelector
bodyID={bodyID}
funcs={funcNames}
onAddNode={onAddNode}
declarationID={declarationID}
/>
</div> </div>
) )
}} }}

View File

@ -45,7 +45,7 @@ export class IFQLPage extends PureComponent<Props, State> {
ast: null, ast: null,
suggestions: [], suggestions: [],
script: script:
'foo = "baz"\n\nfoo = from(db: "telegraf")\n\t|> filter() \n\t|> range(start: -15m)\n\nbar = from(db: "telegraf")\n\t|> filter() \n\t|> range(start: -15m)\n\n', 'baz = "baz"\n\nfoo = from(db: "telegraf")\n\t|> filter() \n\t|> range(start: -15m)\n\nbar = from(db: "telegraf")\n\t|> filter() \n\t|> range(start: -15m)\n\n',
} }
} }
@ -234,7 +234,9 @@ export class IFQLPage extends PureComponent<Props, State> {
declarationID: string declarationID: string
): void => { ): void => {
const script = this.state.body.reduce((acc, body) => { const script = this.state.body.reduce((acc, body) => {
if (body.id === bodyID) { const {id, source, funcs} = body
if (id === bodyID) {
const declaration = body.declarations.find(d => d.id === declarationID) const declaration = body.declarations.find(d => d.id === declarationID)
if (declaration) { if (declaration) {
return `${acc}${declaration.name} = ${this.appendFunc( return `${acc}${declaration.name} = ${this.appendFunc(
@ -243,10 +245,10 @@ export class IFQLPage extends PureComponent<Props, State> {
)}` )}`
} }
return `${acc}${this.appendFunc(body.funcs, name)}` return `${acc}${this.appendFunc(funcs, name)}`
} }
return `${acc}${body.source}` return `${acc}${this.formatSource(source)}`
}, '') }, '')
this.getASTResponse(script) this.getASTResponse(script)
@ -262,7 +264,7 @@ export class IFQLPage extends PureComponent<Props, State> {
const script = this.state.body const script = this.state.body
.map((body, bodyIndex) => { .map((body, bodyIndex) => {
if (body.id !== bodyID) { if (body.id !== bodyID) {
return body.source return this.formatSource(body.source)
} }
const isLast = bodyIndex === this.state.body.length - 1 const isLast = bodyIndex === this.state.body.length - 1
@ -278,24 +280,38 @@ export class IFQLPage extends PureComponent<Props, State> {
const functions = declaration.funcs.filter(f => f.id !== funcID) const functions = declaration.funcs.filter(f => f.id !== funcID)
const s = this.funcsToSource(functions) const s = this.funcsToSource(functions)
return `${declaration.name} = ${this.parseLastSource(s, isLast)}` return `${declaration.name} = ${this.formatLastSource(s, isLast)}`
} }
const funcs = body.funcs.filter(f => f.id !== funcID) const funcs = body.funcs.filter(f => f.id !== funcID)
const source = this.funcsToSource(funcs) const source = this.funcsToSource(funcs)
return this.parseLastSource(source, isLast) return this.formatLastSource(source, isLast)
}) })
.join('') .join('')
this.getASTResponse(script) this.getASTResponse(script)
} }
private formatSource = (source: string): string => {
// currently a bug in the AST which does not add newlines to literal variable assignment bodies
if (!source.match(/\n\n/)) {
return `${source}\n\n`
}
return `${source}`
}
// formats the last line of a body string to include two new lines // formats the last line of a body string to include two new lines
private parseLastSource = (source: string, isLast: boolean): string => { private formatLastSource = (source: string, isLast: boolean): string => {
if (isLast) { if (isLast) {
return `${source}` return `${source}`
} }
// currently a bug in the AST which does not add newlines to literal variable assignment bodies
if (!source.match(/\n\n/)) {
return `${source}\n\n`
}
return `${source}\n\n` return `${source}\n\n`
} }

View File

@ -14,7 +14,6 @@
width: auto; width: auto;
display: flex; display: flex;
color: $ix-text-default; color: $ix-text-default;
text-transform: uppercase;
margin-bottom: $ix-marg-a; margin-bottom: $ix-marg-a;
font-family: $ix-text-font; font-family: $ix-text-font;
font-weight: 500; font-weight: 500;