@@ -12,6 +12,8 @@ import { styled } from '@mui/material/styles';
1212import FileCopyRoundedIcon from '@mui/icons-material/FileCopyRounded' ;
1313import CheckRoundedIcon from '@mui/icons-material/CheckRounded' ;
1414import PropTypes from 'prop-types' ;
15+ import { startCompletion } from '@codemirror/autocomplete' ;
16+ import { format } from 'sql-formatter' ;
1517
1618import gettext from 'sources/gettext' ;
1719import { PgIconButton } from '../Buttons' ;
@@ -22,6 +24,8 @@ import Editor from './components/Editor';
2224import CustomPropTypes from '../../custom_prop_types' ;
2325import FindDialog from './components/FindDialog' ;
2426import GotoDialog from './components/GotoDialog' ;
27+ import usePreferences from '../../../../preferences/static/js/store' ;
28+ import { toCodeMirrorKey } from '../../utils' ;
2529
2630const Root = styled ( 'div' ) ( ( ) => ( {
2731 position : 'relative' ,
@@ -64,25 +68,71 @@ export default function CodeMirror({className, currEditor, showCopyBtn=false, cu
6468 const [ [ showFind , isReplace , findKey ] , setShowFind ] = useState ( [ false , false , false ] ) ;
6569 const [ showGoto , setShowGoto ] = useState ( false ) ;
6670 const [ showCopy , setShowCopy ] = useState ( false ) ;
71+ const preferences = usePreferences ( ) . getPreferencesForModule ( 'sqleditor' ) ;
72+
73+ const formatSQL = ( view ) => {
74+ let selection = true , sql = view . getSelection ( ) ;
75+ /* New library does not support capitalize casing
76+ so if a user has set capitalize casing we will
77+ use preserve casing which is default for the library.
78+ */
79+ let formatPrefs = {
80+ language : 'postgresql' ,
81+ keywordCase : preferences . keyword_case === 'capitalize' ? 'preserve' : preferences . keyword_case ,
82+ identifierCase : preferences . identifier_case === 'capitalize' ? 'preserve' : preferences . identifier_case ,
83+ dataTypeCase : preferences . data_type_case ,
84+ functionCase : preferences . function_case ,
85+ logicalOperatorNewline : preferences . logical_operator_new_line ,
86+ expressionWidth : preferences . expression_width ,
87+ linesBetweenQueries : preferences . lines_between_queries ,
88+ tabWidth : preferences . tab_size ,
89+ useTabs : ! preferences . use_spaces ,
90+ denseOperators : ! preferences . spaces_around_operators ,
91+ newlineBeforeSemicolon : preferences . new_line_before_semicolon
92+ } ;
93+ if ( sql == '' ) {
94+ sql = view . getValue ( ) ;
95+ selection = false ;
96+ }
97+ let formattedSql = format ( sql , formatPrefs ) ;
98+ if ( selection ) {
99+ view . replaceSelection ( formattedSql ) ;
100+ } else {
101+ view . setValue ( formattedSql ) ;
102+ }
103+ } ;
67104
68105 const finalCustomKeyMap = useMemo ( ( ) => [ {
69- key : 'Mod-f' , run : ( ) => {
106+ key : toCodeMirrorKey ( preferences . find ) , run : ( ) => {
70107 setShowFind ( prevVal => [ true , false , ! prevVal [ 2 ] ] ) ;
71108 } ,
72109 preventDefault : true ,
73110 stopPropagation : true ,
74111 } , {
75- key : 'Mod-Alt-f' , run : ( ) => {
112+ key : toCodeMirrorKey ( preferences . replace ) , run : ( ) => {
76113 setShowFind ( prevVal => [ true , true , ! prevVal [ 2 ] ] ) ;
77114 } ,
78115 preventDefault : true ,
79116 stopPropagation : true ,
80117 } , {
81- key : 'Mod-l' , run : ( ) => {
118+ key : toCodeMirrorKey ( preferences . goto_line_col ) , run : ( ) => {
82119 setShowGoto ( true ) ;
83120 } ,
84121 preventDefault : true ,
85122 stopPropagation : true ,
123+ } , {
124+ key : toCodeMirrorKey ( preferences . comment ) , run : ( ) => {
125+ editor . current ?. execCommand ( 'toggleComment' ) ;
126+ } ,
127+ preventDefault : true ,
128+ stopPropagation : true ,
129+ } , {
130+ key : toCodeMirrorKey ( preferences . format_sql ) , run : formatSQL ,
131+ preventDefault : true ,
132+ stopPropagation : true ,
133+ } , {
134+ key : toCodeMirrorKey ( preferences . autocomplete ) , run : startCompletion ,
135+ preventDefault : true ,
86136 } ,
87137 ...customKeyMap ] , [ customKeyMap ] ) ;
88138
@@ -148,5 +198,5 @@ CodeMirror.propTypes = {
148198 className : CustomPropTypes . className ,
149199 showCopyBtn : PropTypes . bool ,
150200 customKeyMap : PropTypes . array ,
151- onTextSelect :PropTypes . func
201+ onTextSelect :PropTypes . func ,
152202} ;
0 commit comments