Skip to content

Commit

Permalink
Fixed new line indentation in query editor and add a user preference …
Browse files Browse the repository at this point in the history
…to disable it. pgadmin-org#7295
  • Loading branch information
adityatoshniwal committed Jun 13, 2024
1 parent e03d65d commit ad34ee2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
Binary file modified docs/en_US/images/preferences_sql_editor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
bracketMatching,
indentUnit,
foldKeymap,
indentService
} from '@codemirror/language';

import syntaxHighlighting from '../extensions/highlighting';
Expand All @@ -46,6 +47,7 @@ import CustomEditorView from '../CustomEditorView';
import breakpointGutter, { breakpointEffect } from '../extensions/breakpointGutter';
import activeLineExtn from '../extensions/activeLineMarker';
import currentQueryHighlighterExtn from '../extensions/currentQueryHighlighter';
import { indentNewLine } from '../extensions/extraStates';

const arrowRightHtml = ReactDOMServer.renderToString(<KeyboardArrowRightRoundedIcon style={{fontSize: '1.2em'}} />);
const arrowDownHtml = ReactDOMServer.renderToString(<ExpandMoreRoundedIcon style={{fontSize: '1.2em'}} />);
Expand Down Expand Up @@ -134,7 +136,15 @@ const defaultExtensions = [
drop: handleDrop,
paste: handlePaste,
}),
errorMarkerExtn()
errorMarkerExtn(),
indentService.of((context, pos) => {
if(context.state.facet(indentNewLine)) {
const previousLine = context.lineAt(pos, -1);
let prevText = previousLine.text.replaceAll('\t', ' '.repeat(context.state.tabSize));
return prevText.match(/^\s*/)?.[0].length;
}
return 0;
}),
];

export default function Editor({
Expand Down Expand Up @@ -310,14 +320,20 @@ export default function Editor({
);
if (pref.use_spaces) {
newConfigExtn.push(
indentUnit.of(new Array(pref.tab_size).fill(' ').join('')),
indentUnit.of(' '.repeat(pref.tab_size)),
);
} else {
newConfigExtn.push(
indentUnit.of('\t'),
);
}

if(pref.indent_new_line) {
newConfigExtn.push(indentNewLine.of(true));
} else {
newConfigExtn.push(indentNewLine.of(false));
}

if (pref.wrap_code) {
newConfigExtn.push(
EditorView.lineWrapping
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2023, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////

import { Facet } from '@codemirror/state';

export const indentNewLine = Facet.define({
combine: values => values.length ? values[0] : true,
});
10 changes: 10 additions & 0 deletions web/pgadmin/tools/sqleditor/utils/query_tool_preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,16 @@ def register_query_tool_preferences(self):
)
)

self.sql_font_size = self.preference.register(
'Editor', 'indent_new_line',
gettext("Auto-indent new line?"), 'boolean', True,
category_label=PREF_LABEL_EDITOR,
help_str=gettext(
'Specifies whether the newly added line using enter key should '
'be auto-indented or not'
)
)

self.expression_width = self.preference.register(
'editor', 'expression_width',
gettext("Expression Width"), 'integer', 50,
Expand Down

0 comments on commit ad34ee2

Please sign in to comment.