mirror of
https://github.com/usebruno/bruno.git
synced 2026-07-02 00:54:09 +00:00
fix:prevent JS hint leak on Ctrl+Space and show allowed root hints (#6776)
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
import React from 'react';
|
||||
import { isEqual, escapeRegExp } from 'lodash';
|
||||
import { defineCodeMirrorBrunoVariablesMode } from 'utils/common/codemirror';
|
||||
import { setupAutoComplete } from 'utils/codemirror/autocomplete';
|
||||
import { setupAutoComplete, showRootHints } from 'utils/codemirror/autocomplete';
|
||||
import StyledWrapper from './StyledWrapper';
|
||||
import * as jsonlint from '@prantlf/jsonlint';
|
||||
import { JSHINT } from 'jshint';
|
||||
@@ -111,8 +111,12 @@ export default class CodeEditor extends React.Component {
|
||||
: cm.replaceSelection(' ', 'end');
|
||||
},
|
||||
'Shift-Tab': 'indentLess',
|
||||
'Ctrl-Space': 'autocomplete',
|
||||
'Cmd-Space': 'autocomplete',
|
||||
'Ctrl-Space': (cm) => {
|
||||
showRootHints(cm, this.props.showHintsFor);
|
||||
},
|
||||
'Cmd-Space': (cm) => {
|
||||
showRootHints(cm, this.props.showHintsFor);
|
||||
},
|
||||
'Ctrl-Y': 'foldAll',
|
||||
'Cmd-Y': 'foldAll',
|
||||
'Ctrl-I': 'unfoldAll',
|
||||
|
||||
@@ -295,9 +295,14 @@ const calculateWordReplacementPositions = (cursor, start, end, word) => {
|
||||
* @returns {string} The determined context
|
||||
*/
|
||||
const determineWordContext = (word) => {
|
||||
if (word.startsWith('req') || word.startsWith('res') || word.startsWith('bru')) {
|
||||
const isApiHint = Object.keys(STATIC_API_HINTS).some(
|
||||
(apiRoot) => apiRoot.toLowerCase().startsWith(word.toLowerCase()) || word.toLowerCase().startsWith(apiRoot.toLowerCase())
|
||||
);
|
||||
|
||||
if (isApiHint) {
|
||||
return 'api';
|
||||
}
|
||||
|
||||
return 'anyword';
|
||||
};
|
||||
|
||||
@@ -514,6 +519,34 @@ const createStandardHintList = (filteredHints, from, to) => {
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Show root-level API hints when the editor is empty
|
||||
* @param {Object} cm - CodeMirror instance
|
||||
* @param {string[]} showHintsFor - Array of hint types to show (e.g., ['req', 'res', 'bru'])
|
||||
* @returns {boolean} True if hints were shown, false otherwise
|
||||
*/
|
||||
export const showRootHints = (cm, showHintsFor = []) => {
|
||||
const wordInfo = getCurrentWordWithContext(cm);
|
||||
// If user is currently typing a word, let handleKeyupForAutocomplete
|
||||
// handle it instead of showing root hints.
|
||||
if (wordInfo) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const hints = Object.keys(STATIC_API_HINTS).filter((rootHint) => showHintsFor.includes(rootHint));
|
||||
|
||||
if (hints.length === 0) return false;
|
||||
|
||||
const cursor = cm.getCursor();
|
||||
const hintList = createStandardHintList(hints, cursor, cursor);
|
||||
|
||||
cm.showHint({
|
||||
hint: () => hintList,
|
||||
completeSingle: false
|
||||
});
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Bruno AutoComplete Helper - Main function with context awareness
|
||||
* @param {Object} cm - CodeMirror instance
|
||||
@@ -625,7 +658,8 @@ const handleKeyupForAutocomplete = (cm, event, options) => {
|
||||
const hints = getAutoCompleteHints(cm, allVariables, anywordAutocompleteHints, options);
|
||||
|
||||
if (!hints) {
|
||||
if (cm.state.completionActive) {
|
||||
const wordInfo = getCurrentWordWithContext(cm);
|
||||
if (cm.state.completionActive && wordInfo) {
|
||||
cm.state.completionActive.close();
|
||||
}
|
||||
return;
|
||||
|
||||
@@ -482,7 +482,7 @@ describe('Bruno Autocomplete', () => {
|
||||
mockedCodemirror.state.completionActive = mockCompletion;
|
||||
|
||||
mockedCodemirror.getCursor.mockReturnValue({ line: 0, ch: 0 });
|
||||
mockedCodemirror.getLine.mockReturnValue(' ');
|
||||
mockedCodemirror.getLine.mockReturnValue('req.bodyy');
|
||||
mockedCodemirror.getRange.mockReturnValue('');
|
||||
|
||||
const mockEvent = { key: 'a' };
|
||||
|
||||
Reference in New Issue
Block a user