'use strict' ; import * as vscode from 'vscode' ; import { RailsHelper } from './rails_helper' ; import { RailsDefinitionProvider } from './rails_definition' ; import { RailsCompletionItemProvider } from './rails_completion' ; import { ViewDefinitionProvider } from './viewRef' ; import { viewDoc } from './view_doc' ; import { Formatter } from './formatter' ; import * as fs from 'fs' ; import * as readline from 'readline' ; import { gitignores , LocalBundle } from './utils' ; import parseGitignore from 'gitignore-globs' ; import path from 'path' ; import { RailsHover } from './rails_hover' ; const RAILS_MODE = { language : 'ruby' , scheme : 'file' } ; const VIEW_MODE = { pattern : '**/views/**' , scheme : 'file' , } ; let gitignoreWatcher ; function railsNavigation ( ) { if ( ! vscode . window . activeTextEditor ) { return ; } const relativeFileName = vscode . workspace . asRelativePath ( vscode . window . activeTextEditor . document . fileName ) ; const line = vscode . window . activeTextEditor . document . lineAt ( vscode . window . activeTextEditor . selection . active . line ) . text . trim ( ) ; const rh = new RailsHelper ( vscode . window . activeTextEditor . document , relativeFileName , line ) ; rh . showFileList ( ) ; } function registerFormatter ( context ) { console . log ( 'registerFormatter' ) ; const docType = [ 'css.erb' , 'scss.erb' , 'html.erb' ] ; for ( let i = 0 , l = docType . length ; i < l ; i ++ ) { registerDocType ( docType [ i ] ) ; } const formatter = new Formatter ( ) ; context . subscriptions . push ( vscode . commands . registerCommand ( 'erb.formatting' , ( ) => { formatter . beautify ( ) ; } ) ) ; context . subscriptions . push ( vscode . workspace . onWillSaveTextDocument ( ( e ) => { formatter . onSave ( e ) ; } ) ) ; function registerDocType ( type ) { context . subscriptions . push ( vscode . languages . registerDocumentRangeFormattingEditProvider ( type , { provideDocumentRangeFormattingEdits : ( document , range , options , token ) => { const start = new vscode . Position ( 0 , 0 ) ; const end = new vscode . Position ( document . lineCount - 1 , document . lineAt ( document . lineCount - 1 ) . text . length ) ; return formatter . registerBeautify ( new vscode . Range ( start , end ) ) ; } , } ) ) ; } } export function activate ( context ) { context . subscriptions . push ( vscode . commands . registerCommand ( 'Rails:Navigation' , railsNavigation ) ) ; context . subscriptions . push ( vscode . languages . registerDefinitionProvider ( RAILS_MODE , new RailsDefinitionProvider ( ) ) ) ; context . subscriptions . push ( vscode . languages . registerHoverProvider ( RAILS_MODE , new RailsHover ( ) ) ) ; context . subscriptions . push ( vscode . languages . registerCompletionItemProvider ( { scheme : 'file' } , new RailsCompletionItemProvider ( ) , '.' , '"' , ':' , "'" ) ) ; function registerViewDefinitionProvider ( ) { context . subscriptions . push ( vscode . languages . registerDefinitionProvider ( VIEW_MODE , new ViewDefinitionProvider ( ) ) ) ; } function registerViewDocCommand ( ) { context . subscriptions . push ( vscode . commands . registerCommand ( 'Rails:Document' , viewDoc , context ) ) ; } vscode . workspace . findFiles ( 'Gemfile' , LocalBundle , vscode . workspace . workspaceFolders . length ) . then ( async ( uris ) => { if ( uris . length >= 1 ) { for ( const uri of uris ) { const fileAbsPath = uri . fsPath ; const fileStream = fs . createReadStream ( fileAbsPath ) ; const rl = readline . createInterface ( { input : fileStream , crlfDelay : Infinity , } ) ; for await ( const lineText of rl ) { if ( /gem\s+['"]rails['"]/ . test ( lineText ) ) { registerViewDefinitionProvider ( ) ; registerViewDocCommand ( ) ; registerFormatter ( context ) ; console . log ( 'Project Gemfile contains rails' ) ; break ; } } } } } ) ; vscode . workspace . workspaceFolders . map ( ( f ) => { const ws = vscode . workspace . getWorkspaceFolder ( f . uri ) ; const wsName = ws . name ; const file = path . join ( ws . uri . fsPath , '.gitignore' ) ; if ( fs . existsSync ( file ) ) { gitignores [ wsName ] = parseGitignore ( file ) ; } } ) ; gitignoreWatcher = vscode . workspace . createFileSystemWatcher ( '.gitignore' , false , false , false ) ; context . subscriptions . push ( gitignoreWatcher . onDidChange ( ( uri ) => { const ws = vscode . workspace . getWorkspaceFolder ( uri ) ; const wsName = ws . name ; gitignores [ wsName ] = parseGitignore ( uri . fsPath ) ; } ) ) ; } export function deactivate ( ) { }