Skip to content

Commit 70e70c9

Browse files
committed
added docs fixed line length bug
added formatting options to docs fixd line length bug where 0 was ignored because it is falsy #39
1 parent bfab4dd commit 70e70c9

File tree

6 files changed

+126
-9
lines changed

6 files changed

+126
-9
lines changed

docs/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"react-dom": "^16.5.2",
1414
"react-scripts-ts": "3.1.0",
1515
"react-syntax-highlighter": "^9.0.0",
16-
"soql-parser-js": "^0.7.0"
16+
"soql-parser-js": "^0.8.0"
1717
},
1818
"scripts": {
1919
"start": "react-scripts-ts start",

docs/src/components/parse-soql.tsx

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { TextField } from 'office-ui-fabric-react/lib/TextField';
33
import { Checkbox } from 'office-ui-fabric-react/lib/Checkbox';
44
import * as React from 'react';
55
import * as CopyToClipboard from 'react-copy-to-clipboard';
6-
import { parseQuery, Query, composeQuery, isQueryValid } from 'soql-parser-js';
6+
import { parseQuery, Query, composeQuery, isQueryValid, FormatOptions } from 'soql-parser-js';
77
import SyntaxHighlighter from 'react-syntax-highlighter/prism';
88
import { xonokai } from 'react-syntax-highlighter/styles/prism';
99

@@ -19,6 +19,7 @@ interface IParseSoqlState {
1919
composedQuery?: string;
2020
soql: string;
2121
format: boolean;
22+
formatOptions: FormatOptions;
2223
}
2324

2425
export class ParseSoql extends React.Component<IParseSoqlProps, IParseSoqlState> {
@@ -34,6 +35,11 @@ export class ParseSoql extends React.Component<IParseSoqlProps, IParseSoqlState>
3435
composedQuery,
3536
soql: props.soql || '',
3637
format: true,
38+
formatOptions: {
39+
fieldMaxLineLen: 60,
40+
fieldSubqueryParensOnOwnLine: true,
41+
whereClauseOperatorsIndented: false,
42+
},
3743
};
3844
}
3945

@@ -61,11 +67,11 @@ export class ParseSoql extends React.Component<IParseSoqlProps, IParseSoqlState>
6167
}
6268
};
6369

64-
public parseQuery = (query?: string, format?: boolean) => {
70+
public parseQuery = (query?: string, format?: boolean, formatOptions?: FormatOptions) => {
6571
try {
6672
format = typeof format === 'boolean' ? format : this.state.format;
6773
const parsedSoql: Query = parseQuery(query || this.state.soql);
68-
const composedQuery: string = composeQuery(parsedSoql, { format });
74+
const composedQuery: string = composeQuery(parsedSoql, { format, formatOptions });
6975
this.setState({
7076
parsedSoql: JSON.stringify(parsedSoql, null, 4),
7177
composedQuery,
@@ -85,7 +91,34 @@ export class ParseSoql extends React.Component<IParseSoqlProps, IParseSoqlState>
8591

8692
public toggleFormat = () => {
8793
this.setState({ format: !this.state.format });
88-
this.parseQuery(this.state.soql, !this.state.format);
94+
this.parseQuery(this.state.soql, !this.state.format, this.state.formatOptions);
95+
};
96+
97+
public toggleSubqueryParens = () => {
98+
const formatOptions = {
99+
...this.state.formatOptions,
100+
fieldSubqueryParensOnOwnLine: !this.state.formatOptions.fieldSubqueryParensOnOwnLine,
101+
};
102+
this.setState({ formatOptions });
103+
this.parseQuery(this.state.soql, this.state.format, formatOptions);
104+
};
105+
106+
public toggleWhereClauseIndent = () => {
107+
const formatOptions = {
108+
...this.state.formatOptions,
109+
whereClauseOperatorsIndented: !this.state.formatOptions.whereClauseOperatorsIndented,
110+
};
111+
this.setState({ formatOptions });
112+
this.parseQuery(this.state.soql, this.state.format, formatOptions);
113+
};
114+
115+
public setMaxFieldLen = (ev: React.SyntheticEvent<HTMLInputElement>) => {
116+
const formatOptions = {
117+
...this.state.formatOptions,
118+
fieldMaxLineLen: Math.max(0, Number((ev.target as HTMLInputElement).value)),
119+
};
120+
this.setState({ formatOptions });
121+
this.parseQuery(this.state.soql, this.state.format, formatOptions);
89122
};
90123

91124
public render() {
@@ -156,6 +189,33 @@ export class ParseSoql extends React.Component<IParseSoqlProps, IParseSoqlState>
156189
</SyntaxHighlighter>
157190
<div style={{ margin: 5 }}>
158191
<Checkbox label="Format Output" checked={this.state.format} onChange={this.toggleFormat} />
192+
<div style={{ margin: 5, paddingLeft: 10 }}>
193+
<div style={{ maxWidth: 250 }}>
194+
<TextField
195+
label="Number of characters before fields wrap"
196+
type="number"
197+
value={String(this.state.formatOptions.fieldMaxLineLen)}
198+
onChange={this.setMaxFieldLen}
199+
disabled={!this.state.format}
200+
/>
201+
</div>
202+
<div style={{ marginTop: 5 }}>
203+
<Checkbox
204+
label="Subquery Parenthesis on own line"
205+
checked={this.state.formatOptions.fieldSubqueryParensOnOwnLine}
206+
onChange={this.toggleSubqueryParens}
207+
disabled={!this.state.format}
208+
/>
209+
</div>
210+
<div style={{ marginTop: 5 }}>
211+
<Checkbox
212+
label="Indent items in WHERE clause"
213+
checked={this.state.formatOptions.whereClauseOperatorsIndented}
214+
onChange={this.toggleWhereClauseIndent}
215+
disabled={!this.state.format}
216+
/>
217+
</div>
218+
</div>
159219
</div>
160220
<CopyToClipboard text={this.state.composedQuery}>
161221
<Button

docs/src/components/sample-queries.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ export default class SampleQueries extends React.Component<ISampleQueriesProps,
182182
num: 43,
183183
soql: `SELECT Id, Name FROM Account WHERE Id IN (SELECT AccountId FROM Contact WHERE LastName LIKE 'apple%') AND Id IN (SELECT AccountId FROM Opportunity WHERE isClosed = false)`,
184184
},
185+
{
186+
key: 44,
187+
num: 44,
188+
soql: `SELECT Id, Name, AccountNumber, AccountSource, AnnualRevenue, BillingAddress, BillingCity, BillingCountry, BillingGeocodeAccuracy, ShippingStreet, Sic, SicDesc, Site, SystemModstamp, TickerSymbol, Type, Website, (SELECT Id, Name, AccountId, Amount, CampaignId, CloseDate, CreatedById, Type FROM Opportunities), (SELECT Id, Name, AccountNumber, AccountSource, AnnualRevenue, BillingAddress, Website FROM ChildAccounts) FROM Account WHERE Name LIKE 'a%' OR Name LIKE 'b%' OR Name LIKE 'c%'`,
189+
},
185190
];
186191
};
187192

lib/SoqlFormatter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { composeQuery } from './SoqlComposer';
22
import { parseQuery } from './SoqlParser';
3+
import { isNumber } from './utils';
34

45
export interface FieldData {
56
fields: {
@@ -85,7 +86,7 @@ export class Formatter {
8586
field.suffix = fieldData.fields.length - 1 === i ? '' : ', ';
8687
lineLen = 0;
8788
newLineAndIndentNext = true;
88-
} else if (this.options.fieldMaxLineLen) {
89+
} else if (isNumber(this.options.fieldMaxLineLen)) {
8990
// If max line length is specified, create a new line when needed
9091
// Add two to account for ", "
9192
lineLen += field.text.length + field.suffix.length;

test/TestCasesForFormat.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,57 @@ AND Id IN (SELECT AccountId
248248
\tFROM Opportunity
249249
\tWHERE isClosed = false)
250250
ORDER BY GROUPING(Type), GROUPING(Id, BillingCountry), Name DESC NULLS FIRST, Id ASC NULLS LAST
251+
`.trim(),
252+
},
253+
{
254+
testCase: 11,
255+
soql: `SELECT Id, Name, AccountNumber, AccountSource, AnnualRevenue, BillingAddress, BillingCity, BillingCountry, BillingGeocodeAccuracy, ShippingStreet, Sic, SicDesc, Site, SystemModstamp, TickerSymbol, Type, Website, (SELECT Id, Name, AccountId, Amount, CampaignId, CloseDate, CreatedById, Type FROM Opportunities), (SELECT Id, Name, AccountNumber, AccountSource, AnnualRevenue, BillingAddress, Website FROM ChildAccounts) FROM Account WHERE Name LIKE 'a%' OR Name LIKE 'b%' OR Name LIKE 'c%'`,
256+
formatOptions: { fieldMaxLineLen: 0, fieldSubqueryParensOnOwnLine: true, whereClauseOperatorsIndented: false },
257+
formattedSoql: `SELECT
258+
\tId,
259+
\tName,
260+
\tAccountNumber,
261+
\tAccountSource,
262+
\tAnnualRevenue,
263+
\tBillingAddress,
264+
\tBillingCity,
265+
\tBillingCountry,
266+
\tBillingGeocodeAccuracy,
267+
\tShippingStreet,
268+
\tSic,
269+
\tSicDesc,
270+
\tSite,
271+
\tSystemModstamp,
272+
\tTickerSymbol,
273+
\tType,
274+
\tWebsite,
275+
\t(
276+
\t\tSELECT
277+
\t\t\tId,
278+
\t\t\tName,
279+
\t\t\tAccountId,
280+
\t\t\tAmount,
281+
\t\t\tCampaignId,
282+
\t\t\tCloseDate,
283+
\t\t\tCreatedById,
284+
\t\t\tType
285+
\t\tFROM Opportunities
286+
\t),
287+
\t(
288+
\t\tSELECT
289+
\t\t\tId,
290+
\t\t\tName,
291+
\t\t\tAccountNumber,
292+
\t\t\tAccountSource,
293+
\t\t\tAnnualRevenue,
294+
\t\t\tBillingAddress,
295+
\t\t\tWebsite
296+
\t\tFROM ChildAccounts
297+
\t)
298+
FROM Account
299+
WHERE Name LIKE 'a%'
300+
OR Name LIKE 'b%'
301+
OR Name LIKE 'c%'
251302
`.trim(),
252303
},
253304
];

0 commit comments

Comments
 (0)