From 2ed35108d76e6209e833a30d09bb977ecafdf22d Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Wed, 17 Apr 2024 14:30:19 +0530 Subject: [PATCH] chore: moved codemirror autocomplete util to its own file --- .../src/components/MultiLineEditor/index.js | 34 ---------------- .../RequestPane/RequestHeaders/index.js | 3 +- .../src/components/SingleLineEditor/index.js | 34 ---------------- packages/bruno-app/src/pages/Bruno/index.js | 1 + .../src/utils/codemirror/autocomplete.js | 40 +++++++++++++++++++ 5 files changed, 42 insertions(+), 70 deletions(-) create mode 100644 packages/bruno-app/src/utils/codemirror/autocomplete.js diff --git a/packages/bruno-app/src/components/MultiLineEditor/index.js b/packages/bruno-app/src/components/MultiLineEditor/index.js index d020580e3..efcd89f45 100644 --- a/packages/bruno-app/src/components/MultiLineEditor/index.js +++ b/packages/bruno-app/src/components/MultiLineEditor/index.js @@ -9,40 +9,6 @@ const SERVER_RENDERED = typeof navigator === 'undefined' || global['PREVENT_CODE if (!SERVER_RENDERED) { CodeMirror = require('codemirror'); - CodeMirror.registerHelper('hint', 'anyword', (editor, options) => { - const word = /[\w$-]+/; - const wordlist = (options && options.autocomplete) || []; - let cur = editor.getCursor(), - curLine = editor.getLine(cur.line); - let end = cur.ch, - start = end; - while (start && word.test(curLine.charAt(start - 1))) --start; - let curWord = start != end && curLine.slice(start, end); - - // Check if curWord is a valid string before proceeding - if (typeof curWord !== 'string' || curWord.length < 3) { - return null; // Abort the hint - } - - const list = (options && options.list) || []; - const re = new RegExp(word.source, 'g'); - for (let dir = -1; dir <= 1; dir += 2) { - let line = cur.line, - endLine = Math.min(Math.max(line + dir * 500, editor.firstLine()), editor.lastLine()) + dir; - for (; line != endLine; line += dir) { - let text = editor.getLine(line), - m; - while ((m = re.exec(text))) { - if (line == cur.line && curWord.length < 3) continue; - list.push(...wordlist.filter((el) => el.toLowerCase().startsWith(curWord.toLowerCase()))); - } - } - } - return { list: [...new Set(list)], from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end) }; - }); - CodeMirror.commands.autocomplete = (cm, hint, options) => { - cm.showHint({ hint, ...options }); - }; } class MultiLineEditor extends Component { diff --git a/packages/bruno-app/src/components/RequestPane/RequestHeaders/index.js b/packages/bruno-app/src/components/RequestPane/RequestHeaders/index.js index 762d9c9ed..445505c07 100644 --- a/packages/bruno-app/src/components/RequestPane/RequestHeaders/index.js +++ b/packages/bruno-app/src/components/RequestPane/RequestHeaders/index.js @@ -7,7 +7,6 @@ import { useTheme } from 'providers/Theme'; import { addRequestHeader, updateRequestHeader, deleteRequestHeader } from 'providers/ReduxStore/slices/collections'; import { sendRequest, saveRequest } from 'providers/ReduxStore/slices/collections/actions'; import SingleLineEditor from 'components/SingleLineEditor'; -import MultiLineEditor from 'components/MultiLineEditor'; import StyledWrapper from './StyledWrapper'; import { headers as StandardHTTPHeaders } from 'know-your-http-well'; const headerAutoCompleteList = StandardHTTPHeaders.map((e) => e.header); @@ -100,7 +99,7 @@ const RequestHeaders = ({ item, collection }) => { /> - { - const word = /[\w$-]+/; - const wordlist = (options && options.autocomplete) || []; - let cur = editor.getCursor(), - curLine = editor.getLine(cur.line); - let end = cur.ch, - start = end; - while (start && word.test(curLine.charAt(start - 1))) --start; - let curWord = start != end && curLine.slice(start, end); - - // Check if curWord is a valid string before proceeding - if (typeof curWord !== 'string' || curWord.length < 3) { - return null; // Abort the hint - } - - const list = (options && options.list) || []; - const re = new RegExp(word.source, 'g'); - for (let dir = -1; dir <= 1; dir += 2) { - let line = cur.line, - endLine = Math.min(Math.max(line + dir * 500, editor.firstLine()), editor.lastLine()) + dir; - for (; line != endLine; line += dir) { - let text = editor.getLine(line), - m; - while ((m = re.exec(text))) { - if (line == cur.line && curWord.length < 3) continue; - list.push(...wordlist.filter((el) => el.toLowerCase().startsWith(curWord.toLowerCase()))); - } - } - } - return { list: [...new Set(list)], from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end) }; - }); - CodeMirror.commands.autocomplete = (cm, hint, options) => { - cm.showHint({ hint, ...options }); - }; } class SingleLineEditor extends Component { diff --git a/packages/bruno-app/src/pages/Bruno/index.js b/packages/bruno-app/src/pages/Bruno/index.js index 87231a07a..71e24dcfa 100644 --- a/packages/bruno-app/src/pages/Bruno/index.js +++ b/packages/bruno-app/src/pages/Bruno/index.js @@ -41,6 +41,7 @@ if (!SERVER_RENDERED) { require('utils/codemirror/brunoVarInfo'); require('utils/codemirror/javascript-lint'); + require('utils/codemirror/autocomplete'); } export default function Main() { diff --git a/packages/bruno-app/src/utils/codemirror/autocomplete.js b/packages/bruno-app/src/utils/codemirror/autocomplete.js new file mode 100644 index 000000000..f1e88537b --- /dev/null +++ b/packages/bruno-app/src/utils/codemirror/autocomplete.js @@ -0,0 +1,40 @@ +let CodeMirror; +const SERVER_RENDERED = typeof navigator === 'undefined' || global['PREVENT_CODEMIRROR_RENDER'] === true; + +if (!SERVER_RENDERED) { + CodeMirror = require('codemirror'); + CodeMirror.registerHelper('hint', 'anyword', (editor, options) => { + const word = /[\w$-]+/; + const wordlist = (options && options.autocomplete) || []; + let cur = editor.getCursor(), + curLine = editor.getLine(cur.line); + let end = cur.ch, + start = end; + while (start && word.test(curLine.charAt(start - 1))) --start; + let curWord = start != end && curLine.slice(start, end); + + // Check if curWord is a valid string before proceeding + if (typeof curWord !== 'string' || curWord.length < 3) { + return null; // Abort the hint + } + + const list = (options && options.list) || []; + const re = new RegExp(word.source, 'g'); + for (let dir = -1; dir <= 1; dir += 2) { + let line = cur.line, + endLine = Math.min(Math.max(line + dir * 500, editor.firstLine()), editor.lastLine()) + dir; + for (; line != endLine; line += dir) { + let text = editor.getLine(line), + m; + while ((m = re.exec(text))) { + if (line == cur.line && curWord.length < 3) continue; + list.push(...wordlist.filter((el) => el.toLowerCase().startsWith(curWord.toLowerCase()))); + } + } + } + return { list: [...new Set(list)], from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end) }; + }); + CodeMirror.commands.autocomplete = (cm, hint, options) => { + cm.showHint({ hint, ...options }); + }; +}