Skip to content

Commit 8d99080

Browse files
committed
add file_name_without_extension
1 parent 8e60671 commit 8d99080

3 files changed

Lines changed: 93 additions & 36 deletions

File tree

FileHeader.py

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# -*- coding: utf-8 -*-
33
# @Author: lime
44
# @Date: 2013-10-28 13:39:48
5-
# @Last Modified by: Dimitrios Katsaros
6-
# @Last Modified time: 2014-07-28 19:25:26
5+
# @Last Modified by: Lime
6+
# @Last Modified time: 2014-07-31 10:19:46
77

88
import os
99
import sys
@@ -149,6 +149,7 @@ def get_user():
149149

150150
return user
151151

152+
152153
def get_project_name():
153154
'''Get project name'''
154155
project_data = sublime.active_window().project_data()
@@ -158,6 +159,25 @@ def get_project_name():
158159
project = None
159160
return project
160161

162+
163+
def get_file_path(path):
164+
'''Get absolute path of the file'''
165+
166+
return 'undefined' if path is None else path
167+
168+
169+
def get_file_name(path):
170+
'''Get name of the file'''
171+
172+
return 'undefined' if path is None else os.path.basename(path)
173+
174+
175+
def get_file_name_without_extension(file_name):
176+
'''Get name of the file without extension'''
177+
178+
return '.'.join(file_name.split('.')[:-1]) or file_name
179+
180+
161181
def get_time(path):
162182
c_time = m_time = None
163183
try:
@@ -199,26 +219,23 @@ def get_st2_time():
199219

200220
return c_time, m_time
201221

202-
def get_file_name():
203-
path = options.get('path', None)
204-
return 'undefined' if path is None else os.path.basename(path)
205-
206-
def get_path():
207-
path = options.get('path', None)
208-
return 'undefined' if path is None else path
209-
210222
args = Settings().get('Default')
211223
args.update(Settings().get(syntax_type, {}))
212224

213225
format = get_strftime()
214226
c_time, m_time = get_st3_time() if IS_ST3 else get_st2_time()
215-
227+
228+
file_path = get_file_path(options.get('path', None))
229+
file_name = get_file_name(options.get('path', None))
230+
file_name_without_extension = get_file_name_without_extension(file_name)
231+
216232
args.update({
217233
'create_time': c_time.strftime(format),
218234
'last_modified_time': m_time.strftime(format),
219-
'file_name': get_file_name(),
220235
'project_name' : get_project_name(),
221-
'path' : get_path(),
236+
'file_name': file_name,
237+
'file_name_without_extension': file_name_without_extension,
238+
'file_path' : file_path
222239
})
223240

224241
user = get_user()
@@ -528,11 +545,19 @@ def run(self, edit, a, b, strings):
528545
self.view.replace(edit, region, strings)
529546

530547

548+
LAST_MODIFIED_BY = 'LAST_MODIFIED_BY'
549+
LAST_MODIFIED_TIME = 'LAST_MODIFIED_TIME'
550+
FILE_NAME = 'FILE_NAME'
551+
FILE_NAME_WITHOUT_EXTENSION = 'FILE_NAME_WITHOUT_EXTENSION'
552+
FILE_PATH = 'FILE_PATH'
553+
531554
class FileHeaderListener(sublime_plugin.EventListener):
532555

533556
LAST_MODIFIED_BY_REGEX = re.compile('\{\{\s*last_modified_by\s*\}\}')
534557
LAST_MODIFIED_TIME_REGEX = re.compile('\{\{\s*last_modified_time\s*\}\}')
535558
FILE_NAME_REGEX = re.compile('\{\{\s*file_name\s*\}\}')
559+
FILE_NAME_WITHOUT_EXTENSION_REGEX = re.compile('\{\{\s*file_name_without_extension\s*\}\}')
560+
FILE_PATH_REGEX = re.compile('\{\{\s*file_path\s*\}\}')
536561

537562
new_view_id = []
538563

@@ -547,7 +572,6 @@ def time_pattern(self):
547572
return _[choice]
548573

549574
def update_automatically(self, view, what):
550-
what = what.upper()
551575
syntax_type = get_syntax_type(view.file_name())
552576

553577
template = get_template_part(syntax_type, 'header')
@@ -569,12 +593,16 @@ def update_automatically(self, view, what):
569593
break
570594

571595
line_header = re.escape(line_header)
572-
if what == 'LAST_MODIFIED_BY' or what == 'FILE_NAME':
596+
if what == LAST_MODIFIED_BY or what == FILE_NAME or \
597+
what == FILE_NAME_WITHOUT_EXTENSION or what == FILE_PATH:
573598
line_pattern = '%s.*\n' % line_header
574-
elif what == 'LAST_MODIFIED_TIME':
599+
600+
elif what == LAST_MODIFIED_TIME:
575601
line_pattern = '%s\s*%s.*\n' % (line_header, self.time_pattern())
602+
576603
else:
577604
raise KeyError()
605+
578606
break
579607

580608
if line_pattern is not None:
@@ -583,12 +611,20 @@ def update_automatically(self, view, what):
583611
a = _.a + space_start
584612
b = _.b - 1
585613

586-
if what == 'LAST_MODIFIED_BY':
614+
file_name = get_file_name(view.file_name())
615+
file_name_without_extension = get_file_name_without_extension(file_name)
616+
file_path = get_file_path(view.file_name())
617+
618+
if what == LAST_MODIFIED_BY:
587619
strings = get_args(syntax_type)['last_modified_by']
588-
elif what == 'LAST_MODIFIED_TIME':
620+
elif what == LAST_MODIFIED_TIME:
589621
strings = datetime.now().strftime(get_strftime())
590-
elif what == 'FILE_NAME':
591-
strings = 'undefined' if view.file_name() is None else os.path.basename(view.file_name())
622+
elif what == FILE_NAME:
623+
strings = file_name
624+
elif what == FILE_NAME_WITHOUT_EXTENSION:
625+
strings = file_name_without_extension
626+
elif what == FILE_PATH:
627+
strings = file_path
592628

593629
spaces = (index - space_start) * ' '
594630
strings = spaces + strings
@@ -630,11 +666,13 @@ def on_pre_save(self, view):
630666
del FileHeaderListener.new_view_id[index]
631667
else:
632668
if view.is_dirty():
633-
self.update_automatically(view, 'last_modified_by')
634-
self.update_automatically(view, 'last_modified_time')
669+
self.update_automatically(view, LAST_MODIFIED_BY)
670+
self.update_automatically(view, LAST_MODIFIED_TIME)
635671

636672
def on_activated(self, view):
637-
self.update_automatically(view, 'file_name')
673+
block(view, self.update_automatically, view, FILE_NAME)
674+
block(view, self.update_automatically, view, FILE_NAME_WITHOUT_EXTENSION)
675+
block(view, self.update_automatically, view, FILE_PATH)
638676

639677
settings = view.settings()
640678
c_time, _ = get_time(view.file_name())

FileHeader.sublime-settings

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,23 +166,33 @@
166166
167167
Can't be set custom.
168168
169+
- file_path
170+
171+
The absolute path of the current file.
172+
173+
FileHeader will update it automatically when you change it.
174+
175+
Can't be set custom.
176+
169177
- file_name
170178
171-
The name of current file.
179+
The name of current file with extension.
172180
173-
FileHeader will update it automatically when you open it.
181+
FileHeader will update it automatically when you change it.
174182
175183
Can't be set custom.
176184
177-
- project_name
185+
- file_name_without_extension
178186
179-
The project name.
187+
The name of current file without extension.
188+
189+
FileHeader will update it automatically when you change it.
180190
181191
Can't be set custom.
182192
183-
- path
193+
- project_name
184194
185-
The absolute path of the current file.
195+
The project name.
186196
187197
Can't be set custom.
188198
*/

README.rst

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,26 +265,35 @@ There are two kinds of arguments: **options** and kinds of languages variables s
265265

266266
Can't be set custom.
267267

268-
- file_name
268+
- file_path
269269

270-
The name of current file.
270+
The absolute path of the current file.
271271

272-
FileHeader will update it automatically when you open it.
272+
FileHeader will update it automatically when you change it.
273273

274274
Can't be set custom.
275275

276-
- project_name
276+
- file_name
277277

278-
The project name.
278+
The name of current file with extension.
279+
280+
FileHeader will update it automatically when you change it.
279281

280282
Can't be set custom.
281283

282-
- path
284+
- file_name_without_extension
283285

284-
The absolute path of the current file.
286+
The name of current file without extension.
287+
288+
FileHeader will update it automatically when you change it.
285289

286290
Can't be set custom.
287291

292+
- project_name
293+
294+
The project name.
295+
296+
Can't be set custom.
288297
*/
289298
290299
/*

0 commit comments

Comments
 (0)