33import * as vscode from 'vscode' ;
44import axios from "axios" ;
55import config from "./config" ;
6- import { m_get , Expression , FindFile } from './utils/snippets' ;
6+ import { mGetExpressions , Expression , FindFile } from './utils/snippets' ;
77
88// this method is called when your extension is activated
99// your extension is activated the very first time the command is executed
@@ -21,54 +21,54 @@ export function activate(context: vscode.ExtensionContext) {
2121 // @see this question in StackOverflow here https://stackoverflow.com/questions/72554515/go-to-definition-highlight-in-vscode-lsp
2222 const definitionProvider = vscode . languages . registerDefinitionProvider ( 'tpl' , {
2323 async provideDefinition ( document , position , _token ) {
24- const wordRange = document . getWordRangeAtPosition ( position , / (?< = " ) .* ?(? = " ) / )
25- if ( ! wordRange || wordRange . isEmpty ) return
24+ const wordRange = document . getWordRangeAtPosition ( position , / (?< = " ) .* ?(? = " ) / ) ;
25+ if ( ! wordRange || wordRange . isEmpty ) { return ; } ;
2626
27- const text = document . getText ( wordRange )
28- const lastDotIndex = text . lastIndexOf ( "." )
29- if ( lastDotIndex <= 0 ) return
27+ const text = document . getText ( wordRange ) ;
28+ const lastDotIndex = text . lastIndexOf ( "." ) ;
29+ if ( lastDotIndex <= 0 ) { return ; } ;
3030
31- const ext = text . substring ( lastDotIndex )
32- if ( ! ext ) return
31+ const ext = text . substring ( lastDotIndex + 1 ) ;
32+ if ( ! ext ) { return ; } ;
3333
34- const lastSlashIndex = text . lastIndexOf ( "/" )
35- const fileName = text . substring ( lastSlashIndex ) . replace ( "/" , "" )
36- if ( ! fileName ) return
34+ const lastSlashIndex = text . lastIndexOf ( "/" ) ;
35+ const fileName = text . substring ( lastSlashIndex ) . replace ( "/" , "" ) ;
36+ if ( ! fileName ) { return ; } ;
3737
38- const tplLocationPattern = "**/priv/templates/**/"
39- const jsLocationPattern = "**/priv/lib/**/"
40- const cssLocationPattern = "**/priv/lib/**/"
41- const imageLocationPattern = "**/priv/lib/images/**/"
38+ const tplLocationPattern = "**/priv/templates/**/" ;
39+ const jsLocationPattern = "**/priv/lib/**/" ;
40+ const cssLocationPattern = "**/priv/lib/**/" ;
41+ const imageLocationPattern = "**/priv/lib/images/**/" ;
4242
4343 const extLocationPattern = {
44- ". tpl" : tplLocationPattern ,
45- ".js" : jsLocationPattern ,
46- ". css" : cssLocationPattern ,
47- ". apng" : imageLocationPattern ,
48- ". gif" : imageLocationPattern ,
49- ". ico" : imageLocationPattern ,
50- ". cur" : imageLocationPattern ,
51- ". jpg" : imageLocationPattern ,
52- ". jpeg" : imageLocationPattern ,
53- ". jfif" : imageLocationPattern ,
54- ". pjpeg" : imageLocationPattern ,
55- ". pjp" : imageLocationPattern ,
56- ". png" : imageLocationPattern ,
57- ". svg" : imageLocationPattern ,
58- }
59-
60- const locationPattern = extLocationPattern [ ext as keyof typeof extLocationPattern ]
61- if ( ! locationPattern ) return
44+ tpl : tplLocationPattern ,
45+ js : jsLocationPattern ,
46+ css : cssLocationPattern ,
47+ apng : imageLocationPattern ,
48+ gif : imageLocationPattern ,
49+ ico : imageLocationPattern ,
50+ cur : imageLocationPattern ,
51+ jpg : imageLocationPattern ,
52+ jpeg : imageLocationPattern ,
53+ jfif : imageLocationPattern ,
54+ pjpeg : imageLocationPattern ,
55+ pjp : imageLocationPattern ,
56+ png : imageLocationPattern ,
57+ svg : imageLocationPattern ,
58+ } ;
59+
60+ const locationPattern = extLocationPattern [ ext as keyof typeof extLocationPattern ] ;
61+ if ( ! locationPattern ) { return ; } ;
6262
6363 const filePattern = `${ locationPattern } ${ fileName } ` ;
64- const ignorePattern = "**/_build/**"
64+ const ignorePattern = "**/_build/**" ;
6565
6666 const files = await vscode . workspace . findFiles ( filePattern , ignorePattern ) ;
67- if ( ! files ) return ;
67+ if ( ! files ) { return ; }
6868
6969 return files . map ( uri => new vscode . Location (
7070 uri , new vscode . Position ( 0 , 0 )
71- ) )
71+ ) ) ;
7272 }
7373 } ) ;
7474
@@ -77,17 +77,17 @@ export function activate(context: vscode.ExtensionContext) {
7777 const hoverProvider = vscode . languages . registerHoverProvider ( 'tpl' , {
7878 async provideHover ( document , position , _token ) {
7979 const patternMatch = ( pattern : RegExp ) => {
80- const wordRange = document . getWordRangeAtPosition ( position , pattern )
80+ const wordRange = document . getWordRangeAtPosition ( position , pattern ) ;
8181 return ! ! wordRange && ! wordRange . isEmpty
8282 ? document . getText ( wordRange )
83- : undefined
84- }
83+ : undefined ;
84+ } ;
8585
86- const doc = config . getDoc ( patternMatch )
87- if ( ! doc || ! doc . raw ) return
86+ const doc = config . getDoc ( patternMatch ) ;
87+ if ( ! doc || ! doc . raw ) { return ; } ;
8888
8989 const response = await axios . get ( doc . raw ) ;
90- if ( response . status !== 200 ) return
90+ if ( response . status !== 200 ) { return ; } ;
9191
9292 const markdown = new vscode . MarkdownString ( response . data ) ;
9393 markdown . supportHtml = true ;
@@ -107,78 +107,77 @@ export function activate(context: vscode.ExtensionContext) {
107107 _token : vscode . CancellationToken ,
108108 _context : vscode . CompletionContext
109109 ) {
110- const modelNameRe = / \b m \. ( \w + ) ? /
111- const modelNameRange = document . getWordRangeAtPosition ( position , modelNameRe )
110+ const modelNameRe = / \b m \. ( \w + ) ? / ;
111+ const modelNameRange = document . getWordRangeAtPosition ( position , modelNameRe ) ;
112112 if ( ! ! modelNameRange && ! modelNameRange . isEmpty ) {
113- const modelsPattern = "{apps,apps_user}/**/src/models/**/m_*.erl"
113+ const modelsPattern = "{apps,apps_user}/**/src/models/**/m_*.erl" ;
114114 const models = await vscode . workspace . findFiles ( modelsPattern ) ;
115115 return models . reduce ( ( arr , file ) => {
116- const modelRe = / (?< = \b m _ ) .* ?(? = .e r l ) /
117- const modelMatch = modelRe . exec ( file . fsPath )
116+ const modelRe = / (?< = \b m _ ) .* ?(? = .e r l ) / ;
117+ const modelMatch = modelRe . exec ( file . fsPath ) ;
118118 if ( ! modelMatch || ! modelMatch . length ) {
119- return arr
119+ return arr ;
120120 }
121121
122- const model = modelMatch [ 0 ]
123- const modelExpressionsFinder = ( m : string ) => m_get ( findFile , m )
124- const snippet = new vscode . CompletionItem ( model )
125- snippet . insertText = new vscode . SnippetString ( model )
122+ const model = modelMatch [ 0 ] ;
123+ const modelExpressionsFinder = ( m : string ) => mGetExpressions ( findFile , m ) ;
124+ const snippet = new vscode . CompletionItem ( model ) ;
125+ snippet . insertText = new vscode . SnippetString ( model ) ;
126126 snippet . command = {
127127 command : "tpl.snippet.pick" ,
128128 title : "m_get" ,
129129 arguments : [ model , modelExpressionsFinder ]
130- }
131- arr . push ( snippet )
132- return arr
133- } , new Array < vscode . CompletionItem > ( ) )
130+ } ;
131+ arr . push ( snippet ) ;
132+ return arr ;
133+ } , new Array < vscode . CompletionItem > ( ) ) ;
134134 }
135135
136- const variableRe = / (?< = \[ ) .* ?(? = \] ) | (?< = { ) .* ?(? = } ) | (?< = : ) .* ?(? = } | , ) | (?< = = ) .* ?(? = ( } | , | % } ) ) /
137- const variableRange = document . getWordRangeAtPosition ( position , variableRe )
136+ const variableRe = / (?< = \[ ) .* ?(? = \] ) | (?< = { ) .* ?(? = } ) | (?< = : ) .* ?(? = } | , ) | (?< = = ) .* ?(? = ( } | , | % } ) ) / ;
137+ const variableRange = document . getWordRangeAtPosition ( position , variableRe ) ;
138138 if ( ! ! variableRange ) {
139139 // TODO: Variables snippets.
140140 // It will be awesome if variables can pop up as suggestion.
141- return
141+ return ;
142142 }
143143
144- const mSnippet = new vscode . CompletionItem ( "m" )
145- mSnippet . insertText = new vscode . SnippetString ( "m" )
144+ const mSnippet = new vscode . CompletionItem ( "m" ) ;
145+ mSnippet . insertText = new vscode . SnippetString ( "m" ) ;
146146
147147 return [
148148 mSnippet
149- ]
149+ ] ;
150150 }
151- // })
152- } , "." , "[" , "{" , "|" )
151+ } , "." , "[" , "{" , "|" ) ;
153152
154- context . subscriptions . push ( completionProvider )
153+ context . subscriptions . push ( completionProvider ) ;
155154
156155 vscode . commands . registerCommand ( 'tpl.snippet.pick' , async ( model , modelExpressionsFinder ) => {
157- const expressions : Array < Expression > = await modelExpressionsFinder ( model )
156+ const expressions : Array < Expression > = await modelExpressionsFinder ( model ) ;
158157 if ( expressions instanceof Error ) {
159- await vscode . window . showErrorMessage ( expressions . message )
160- return undefined
158+ await vscode . window . showErrorMessage ( expressions . message ) ;
159+ return undefined ;
161160 }
162161
163162 const quickPick = vscode . window . createQuickPick ( ) ;
164163 quickPick . items = expressions . map ( ( { expression : label } ) => ( { label } ) ) ;
165164 quickPick . onDidChangeSelection ( async ( [ { label } ] ) => {
166- const token = expressions . find ( token => token . expression === label )
165+ const token = expressions . find ( token => token . expression === label ) ;
167166 if ( ! token ) {
168- throw ( new Error ( `Unexpected no token match in quick pick with label '${ label } '` ) )
167+ throw ( new Error ( `Unexpected no token match in quick pick with label '${ label } '` ) ) ;
169168 }
170169
171- await vscode . commands . executeCommand ( "tpl.snippet.insert" , token . snippet )
170+ await vscode . commands . executeCommand ( "tpl.snippet.insert" , token . snippet ) ;
172171 quickPick . hide ( ) ;
173172 } ) ;
174173 quickPick . show ( ) ;
175- } )
174+ } ) ;
176175
177176 vscode . commands . registerTextEditorCommand ( 'tpl.snippet.insert' , ( editor , _edit , snippet ) => {
178177 return editor . insertSnippet (
179178 new vscode . SnippetString ( snippet ) ,
180179 ) ;
181- } )
180+ } ) ;
182181}
183182
184183// this method is called when your extension is deactivated
@@ -190,5 +189,5 @@ const findFile: FindFile = async (pattern, ignorePattern = null) => {
190189 const files = await vscode . workspace . findFiles ( pattern , ignorePattern , 1 ) ;
191190 return files . length
192191 ? files [ 0 ] . fsPath
193- : undefined
194- }
192+ : undefined ;
193+ } ;
0 commit comments