<commit_before>" clear spaces at EOF and tabs at end of not empty lines
"Author: <NAME>

let g:auto_clear_spaces_at_eof = 1

function! ClearSpacesAtEOF(doItAnyway)
	
	" clear spaces and tabs at EOF of not empty lines
	
	if g:auto_clear_spaces_at_eof == 0 && a:doItAnyway == 0
		return
	endif
	
	" current line
	let s:i = 0
	" lines count
	let s:c = line('$')
	" is multiline comment opened
	let s:isoc = 0
	
	while s:i < s:c
		
		let s:i        = s:i + 1
		let s:line     = getline(s:i)
		let s:lineorig = s:line
		
		if   &filetype == 'javascript'
		\ || &filetype == 'ls'
			
			let s:hasComments = 1
			
			" /* */ comments
			while s:hasComments == 1
				let s:hasComments = 0
				if s:isoc == 0
					if s:line =~ '/\*'
						let s:hasComments = 1
						let s:isoc = 1
						let s:linesub = substitute(
							\ s:line,
							\ '^.\{-}/\*.\{-}\*/', 'x', '')
						" if previous regex didn't match
						if s:linesub == s:line
							let s:line = ''
						else
							let s:line = s:linesub
							let s:isoc = 0
						endif
					endif
				else
					if s:line =~ '\*/'
						let s:hasComments = 1
						let s:isoc = 0
						let s:line = substitute(s:line, '^.\{-}\*/', 'x', '')
					endif
				endif
			endwhile
			
			if &filetype == 'ls'
				" # comments
				let s:line = substitute(s:line, '[ \t]*#.*$', '', '')
			else "javascript
				" // comments
				let s:line = substitute(s:line, '[ \t]*//.*$', '', '')
			endif
		endif
		
		if s:isoc == 0 && s:line =~ '\([^ \t]\)[ \t]\+$'
			call setline(
				\ s:i,
				\ substitute(s:lineorig, '\([^ \t]\)[ \t]\+$', '\1', '')
			\ )
		endif
	endwhile
	
	" clear all spaces at EOF if we using tabs for indentation
	if ! &expandtab
		try
			silent %s/[ ]\+$//g
		catch
		endtry
	endif
	
	" clear tabs after spaces if we using tabs for indentation
	if ! &expandtab
		try
			silent %s/[ ]\+[ \t]\+$//g
		catch
		endtry
	endif
	
endfunction

function! AutoClearSpacesAtEOF(status)
	if a:status == -1 " toggle
		if g:auto_clear_spaces_at_eof == 0
			call AutoClearSpacesAtEOF(1)
		else
			call AutoClearSpacesAtEOF(0)
		endif
	elseif a:status == 0 " disable
		let g:auto_clear_spaces_at_eof = 0
		echo 'Auto clear spaces at EOF is disabled'
	elseif a:status == 1 " enable<commit_msg>restoring cursor position for clear-spaces-at-eof.vim<commit_after>" clear spaces at EOF and tabs at end of not empty lines
"Author: <NAME>

let g:auto_clear_spaces_at_eof = 1

function! ClearSpacesAtEOF(doItAnyway)
	
	" clear spaces and tabs at EOF of not empty lines
	
	if g:auto_clear_spaces_at_eof == 0 && a:doItAnyway == 0
		return
	endif
	
	let oldPosLine = line('.')
	let oldPosCol  = col('.')
	
	" current line
	let s:i = 0
	" lines count
	let s:c = line('$')
	" is multiline comment opened
	let s:isoc = 0
	
	while s:i < s:c
		
		let s:i        = s:i + 1
		let s:line     = getline(s:i)
		let s:lineorig = s:line
		
		if   &filetype == 'javascript'
		\ || &filetype == 'ls'
			
			let s:hasComments = 1
			
			" /* */ comments
			while s:hasComments == 1
				let s:hasComments = 0
				if s:isoc == 0
					if s:line =~ '/\*'
						let s:hasComments = 1
						let s:isoc = 1
						let s:linesub = substitute(
							\ s:line,
							\ '^.\{-}/\*.\{-}\*/', 'x', '')
						" if previous regex didn't match
						if s:linesub == s:line
							let s:line = ''
						else
							let s:line = s:linesub
							let s:isoc = 0
						endif
					endif
				else
					if s:line =~ '\*/'
						let s:hasComments = 1
						let s:isoc = 0
						let s:line = substitute(s:line, '^.\{-}\*/', 'x', '')
					endif
				endif
			endwhile
			
			if &filetype == 'ls'
				" # comments
				let s:line = substitute(s:line, '[ \t]*#.*$', '', '')
			else "javascript
				" // comments
				let s:line = substitute(s:line, '[ \t]*//.*$', '', '')
			endif
		endif
		
		if s:isoc == 0 && s:line =~ '\([^ \t]\)[ \t]\+$'
			call setline(
				\ s:i,
				\ substitute(s:lineorig, '\([^ \t]\)[ \t]\+$', '\1', '')
			\ )
		endif
	endwhile
	
	" clear all spaces at EOF if we using tabs for indentation
	if ! &expandtab
		try
			silent %s/[ ]\+$//g
		catch
		endtry
	endif
	
	" clear tabs after spaces if we using tabs for indentation
	if ! &expandtab
		try
			silent %s/[ ]\+[ \t]\+$//g
		catch
		endtry
	endif
	
	call cursor(oldPosLine, oldPosCol)
	
endfunction

function! AutoClearSpacesAtEOF(status)
	if a:status == -1 " toggle
		if g:auto_clear_spaces_at_eof == 0
			call AutoClearSpacesAtEOF(1)
		else
			call AutoClearSpacesAtEOF(0)
		endif
	elseif a:status == 0 " disable
		let g:auto_clear_spaces_at_eof = 0
		echo 'Auto clear spaces at EOF is disabled'
	elseif a:status == 1 " enable
{'fraction_non_alphanumeric': 0.15413124046217572, 'fraction_numerical': 0.010464355788096796, 'mean_word_length': 2.277142857142857, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6424544', 'n_tokens_mistral': 2211, 'n_tokens_neox': 1714, 'n_words': 490}