Skip to content

Commit cfb38ef

Browse files
committed
Refactor moved get hints function to a util and removed unnecessary changes
1 parent 5aa21d9 commit cfb38ef

File tree

4 files changed

+31
-53
lines changed

4 files changed

+31
-53
lines changed

packages/bruno-app/src/components/CodeEditor/index.js

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,16 @@
88
import React from 'react';
99
import { isEqual, escapeRegExp } from 'lodash';
1010
import { defineCodeMirrorBrunoVariablesMode } from 'utils/common/codemirror';
11+
import { getMockDataHints } from 'utils/codemirror/mock-data-hints';
1112
import StyledWrapper from './StyledWrapper';
1213
import * as jsonlint from '@prantlf/jsonlint';
1314
import { JSHINT } from 'jshint';
1415
import stripJsonComments from 'strip-json-comments';
1516
import { getAllVariables } from 'utils/collections';
1617

17-
import { mockDataFunctions } from '@usebruno/common';
18-
1918
let CodeMirror;
2019
const SERVER_RENDERED = typeof window === 'undefined' || global['PREVENT_CODEMIRROR_RENDER'] === true;
2120
const TAB_SIZE = 2;
22-
const MOCK_FUNCTION_SUGGESTIONS = Object.keys(mockDataFunctions).map(key => `$${key}`);
2321

2422
if (!SERVER_RENDERED) {
2523
CodeMirror = require('codemirror');
@@ -91,7 +89,7 @@ if (!SERVER_RENDERED) {
9189
'bru.runner.stopExecution()'
9290
];
9391

94-
CodeMirror.registerHelper('hint', 'brunoJS', (editor, _options) => {
92+
CodeMirror.registerHelper('hint', 'brunoJS', (editor, options) => {
9593
const cursor = editor.getCursor();
9694
const currentLine = editor.getLine(cursor.line);
9795
let startBru = cursor.ch;
@@ -307,30 +305,8 @@ export default class CodeEditor extends React.Component {
307305
});
308306
}
309307

310-
const getHints = (cm) => {
311-
const cursor = cm.getCursor();
312-
const currentString = cm.getRange({ line: cursor.line, ch: 0 }, cursor);
313-
314-
const match = currentString.match(/\{\{\$(\w*)$/);
315-
if (!match) return null;
316-
317-
const wordMatch = match[1];
318-
if (!wordMatch) return null;
319-
320-
const suggestions = MOCK_FUNCTION_SUGGESTIONS.filter(name => name.startsWith(`$${wordMatch}`));
321-
if (!suggestions.length) return null;
322-
323-
const startPos = { line: cursor.line, ch: currentString.lastIndexOf('{{$') + 2 }; // +2 accounts for `{{
324-
325-
return {
326-
list: suggestions,
327-
from: startPos,
328-
to: cm.getCursor(),
329-
};
330-
};
331-
332308
editor.on('inputRead', function (cm, event) {
333-
const hints = getHints(cm);
309+
const hints = getMockDataHints(cm);
334310
if (!hints) {
335311
return;
336312
}

packages/bruno-app/src/components/SingleLineEditor/index.js

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { Component } from 'react';
22
import isEqual from 'lodash/isEqual';
33
import { getAllVariables } from 'utils/collections';
44
import { defineCodeMirrorBrunoVariablesMode, MaskedEditor } from 'utils/common/codemirror';
5+
import { getMockDataHints } from 'utils/codemirror/mock-data-hints';
56
import StyledWrapper from './StyledWrapper';
67
import { IconEye, IconEyeOff } from '@tabler/icons';
78

@@ -89,30 +90,8 @@ class SingleLineEditor extends Component {
8990
});
9091
}
9192

92-
const getHints = (cm) => {
93-
const cursor = cm.getCursor();
94-
const currentString = cm.getRange({ line: cursor.line, ch: 0 }, cursor);
95-
96-
const match = currentString.match(/\{\{\$(\w*)$/);
97-
if (!match) return null;
98-
99-
const wordMatch = match[1];
100-
if (!wordMatch) return null;
101-
102-
const suggestions = MOCK_FUNCTION_SUGGESTIONS.filter(name => name.startsWith(`$${wordMatch}`));
103-
if (!suggestions.length) return null;
104-
105-
const startPos = { line: cursor.line, ch: currentString.lastIndexOf('{{$') + 2 }; // +2 accounts for `{{`
106-
107-
return {
108-
list: suggestions,
109-
from: startPos,
110-
to: cm.getCursor(),
111-
};
112-
};
113-
11493
this.editor.on('inputRead', function (cm, event) {
115-
const hints = getHints(cm);
94+
const hints = getMockDataHints(cm);
11695
if (!hints) {
11796
return;
11897
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { mockDataFunctions } from '@usebruno/common';
2+
3+
const MOCK_FUNCTION_SUGGESTIONS = Object.keys(mockDataFunctions).map(key => `$${key}`);
4+
5+
export const getMockDataHints = (cm) => {
6+
const cursor = cm.getCursor();
7+
const currentString = cm.getRange({ line: cursor.line, ch: 0 }, cursor);
8+
9+
const match = currentString.match(/\{\{\$(\w*)$/);
10+
if (!match) return null;
11+
12+
const wordMatch = match[1];
13+
if (!wordMatch) return null;
14+
15+
const suggestions = MOCK_FUNCTION_SUGGESTIONS.filter(name => name.startsWith(`$${wordMatch}`));
16+
if (!suggestions.length) return null;
17+
18+
const startPos = { line: cursor.line, ch: currentString.lastIndexOf('{{$') + 2 }; // +2 accounts for `{{`
19+
20+
return {
21+
list: suggestions,
22+
from: startPos,
23+
to: cm.getCursor(),
24+
};
25+
};

packages/bruno-common/src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
import { mockDataFunctions } from './utils/faker-functions';
2-
3-
export { mockDataFunctions };
1+
export { mockDataFunctions } from './utils/faker-functions';
42
export { default as interpolate } from './interpolate';

0 commit comments

Comments
 (0)