@@ -12,6 +12,8 @@ import { registerTaskProvider } from './task/rake';
1212import { Config as LintConfig } from './lint/lintConfig' ;
1313import * as debounce from 'lodash/debounce' ;
1414
15+ import { registerHighlightProvider } from './providers/highlight' ;
16+
1517export function activate ( context : ExtensionContext ) {
1618 const subs = context . subscriptions ;
1719 // register language config
@@ -52,72 +54,6 @@ function getGlobalLintConfig() : LintConfig {
5254 return globalConfig ;
5355}
5456
55- function registerHighlightProvider ( ctx : ExtensionContext ) {
56- // highlight provider
57- let pairedEnds = [ ] ;
58-
59- const getEnd = function ( line ) {
60- //end must be on a line by itself, or followed directly by a dot
61- let match = line . text . match ( / ^ ( \s * ) e n d \b [ \. \s # ] ? \s * $ / ) ;
62- if ( match ) {
63- return new vscode . Range ( line . lineNumber , match [ 1 ] . length , line . lineNumber , match [ 1 ] . length + 3 ) ;
64- }
65- }
66-
67- const getEntry = function ( line ) {
68- let match = line . text . match ( / ^ ( .* \b ) ( b e g i n | c l a s s | d e f | f o r | i f | m o d u l e | u n l e s s | u n t i l | c a s e | w h i l e ) \b [ ^ ; ] * $ / ) ;
69- if ( match ) {
70- return new vscode . Range ( line . lineNumber , match [ 1 ] . length , line . lineNumber , match [ 1 ] . length + match [ 2 ] . length ) ;
71- } else {
72- //check for do
73- match = line . text . match ( / \b ( d o ) \b \s * ( \| .* \| [ ^ ; ] * ) ? $ / ) ;
74- if ( match ) {
75- return new vscode . Range ( line . lineNumber , match . index , line . lineNumber , match . index + 2 ) ;
76- }
77- }
78- }
79-
80- const balancePairs = function ( doc ) {
81- pairedEnds = [ ] ;
82- if ( doc . languageId !== 'ruby' ) return ;
83-
84- let waitingEntries = [ ] ;
85- let entry , end ;
86- for ( let i = 0 ; i < doc . lineCount ; i ++ ) {
87- if ( ( entry = getEntry ( doc . lineAt ( i ) ) ) ) {
88- waitingEntries . push ( entry ) ;
89- } else if ( waitingEntries . length && ( end = getEnd ( doc . lineAt ( i ) ) ) ) {
90- pairedEnds . push ( {
91- entry : waitingEntries . pop ( ) ,
92- end : end
93- } ) ;
94- }
95- }
96- }
97-
98- const balanceEvent = function ( event ) {
99- if ( event && event . document ) balancePairs ( event . document ) ;
100- }
101-
102- ctx . subscriptions . push ( vscode . languages . registerDocumentHighlightProvider ( 'ruby' , {
103- provideDocumentHighlights : ( doc , pos ) => {
104- let result = pairedEnds . find ( pair => (
105- pair . entry . start . line === pos . line ||
106- pair . end . start . line === pos . line ) ) ;
107- if ( result ) {
108- return [ new vscode . DocumentHighlight ( result . entry , 2 ) , new vscode . DocumentHighlight ( result . end , 2 ) ] ;
109- }
110- }
111- } ) ) ;
112-
113- ctx . subscriptions . push ( vscode . window . onDidChangeActiveTextEditor ( balanceEvent ) ) ;
114- ctx . subscriptions . push ( vscode . workspace . onDidChangeTextDocument ( balanceEvent ) ) ;
115- ctx . subscriptions . push ( vscode . workspace . onDidOpenTextDocument ( balancePairs ) ) ;
116- if ( vscode . window && vscode . window . activeTextEditor ) {
117- balancePairs ( vscode . window . activeTextEditor . document ) ;
118- }
119- }
120-
12157function registerLinters ( ctx : ExtensionContext ) {
12258 const globalConfig = getGlobalLintConfig ( ) ;
12359 const linters = new LintCollection ( globalConfig , vscode . workspace . getConfiguration ( "ruby" ) . lint , vscode . workspace . rootPath ) ;
@@ -280,4 +216,4 @@ function registerIntellisenseProvider(ctx: ExtensionContext) {
280216 } ;
281217 ctx . subscriptions . push ( vscode . commands . registerCommand ( 'ruby.reloadProject' , rubyLocateDisabled ) ) ;
282218 }
283- }
219+ }
0 commit comments