@@ -3,34 +3,26 @@ import type { IDisposable } from "monaco-editor";
33import type * as Monaco from "monaco-editor" ;
44import { format as formatSql } from "sql-formatter" ;
55import type { FormatOptionsWithLanguage } from "sql-formatter" ;
6-
7- type UseSqlFormattingProvidersOptions = {
8- languageId : string ;
9- getOptions ?: ( ) => FormatOptionsWithLanguage ;
10- } ;
6+ import { ID_LANGUAGE_SQL } from "@/components/editor.config" ;
117
128const getDefaultSqlFormatOptions = ( ) : FormatOptionsWithLanguage => ( {
139 language : "sqlite" ,
1410 keywordCase : "upper" ,
1511} ) ;
1612
17- export function useSqlFormattingProviders (
18- monaco : typeof Monaco | null ,
19- { languageId, getOptions } : UseSqlFormattingProvidersOptions ,
20- ) {
13+ export function useSqlFormattingProviders ( monaco : typeof Monaco | null ) {
2114 const disposablesRef = useRef < IDisposable [ ] > ( [ ] ) ;
2215
2316 useEffect ( ( ) => {
2417 if ( ! monaco ) return ;
2518
26- // Dispose any previous providers
2719 disposablesRef . current . forEach ( ( d ) => d . dispose ( ) ) ;
2820 disposablesRef . current = [ ] ;
2921
30- const getOpts = getOptions ?? getDefaultSqlFormatOptions ;
22+ const getOpts = getDefaultSqlFormatOptions ;
3123
3224 const documentProvider =
33- monaco . languages . registerDocumentFormattingEditProvider ( languageId , {
25+ monaco . languages . registerDocumentFormattingEditProvider ( ID_LANGUAGE_SQL , {
3426 provideDocumentFormattingEdits : ( model ) => {
3527 const fullRange = model . getFullModelRange ( ) ;
3628 const text = model . getValue ( ) ;
@@ -43,39 +35,39 @@ export function useSqlFormattingProviders(
4335 } ,
4436 ] ;
4537 } catch ( err ) {
46- // Keep UX stable if formatting fails
47- // eslint-disable-next-line no-console
4838 console . error ( "SQL formatting error (document)" , err ) ;
4939 return [ ] ;
5040 }
5141 } ,
5242 } ) ;
5343
5444 const rangeProvider =
55- monaco . languages . registerDocumentRangeFormattingEditProvider ( languageId , {
56- provideDocumentRangeFormattingEdits : ( model , range ) => {
57- const text = model . getValueInRange ( range ) ;
58- try {
59- const formatted = formatSql ( text , getOpts ( ) ) ;
60- return [
61- {
62- range,
63- text : formatted ,
64- } ,
65- ] ;
66- } catch ( err ) {
67- // eslint-disable-next-line no-console
68- console . error ( "SQL formatting error (range)" , err ) ;
69- return [ ] ;
70- }
45+ monaco . languages . registerDocumentRangeFormattingEditProvider (
46+ ID_LANGUAGE_SQL ,
47+ {
48+ provideDocumentRangeFormattingEdits : ( model , range ) => {
49+ const text = model . getValueInRange ( range ) ;
50+ try {
51+ const formatted = formatSql ( text , getOpts ( ) ) ;
52+ return [
53+ {
54+ range,
55+ text : formatted ,
56+ } ,
57+ ] ;
58+ } catch ( err ) {
59+ console . error ( "SQL formatting error (range)" , err ) ;
60+ return [ ] ;
61+ }
62+ } ,
7163 } ,
72- } ) ;
64+ ) ;
7365
7466 disposablesRef . current = [ documentProvider , rangeProvider ] ;
7567
7668 return ( ) => {
7769 disposablesRef . current . forEach ( ( d ) => d . dispose ( ) ) ;
7870 disposablesRef . current = [ ] ;
7971 } ;
80- } , [ monaco , languageId , getOptions ] ) ;
72+ } , [ monaco ] ) ;
8173}
0 commit comments