# HG changeset patch # User Doug Simon # Date 1396015115 -3600 # Node ID 7dfc0e9fd45aaf3aacecda087c2d287bca627cb6 # Parent 00eb80d735eda288ce595e50add2005476eed8e8 added removal of trailing whitespace to eclipseformat to emulate the actions performed by the IDE diff -r 00eb80d735ed -r 7dfc0e9fd45a mxtool/mx.py --- a/mxtool/mx.py Fri Mar 28 12:39:46 2014 +0100 +++ b/mxtool/mx.py Fri Mar 28 14:58:35 2014 +0100 @@ -1949,14 +1949,19 @@ projects = [project(name) for name in args.projects.split(',')] class Batch: - def __init__(self, settingsFile, javaCompliance): - self.path = settingsFile + def __init__(self, settingsDir, javaCompliance): + self.path = join(settingsDir, 'org.eclipse.jdt.core.prefs') self.javaCompliance = javaCompliance self.javafiles = list() + with open(join(settingsDir, 'org.eclipse.jdt.ui.prefs')) as fp: + jdtUiPrefs = fp.read() + self.removeTrailingWhitespace = 'sp_cleanup.remove_trailing_whitespaces_all=true' in jdtUiPrefs + if self.removeTrailingWhitespace: + assert 'sp_cleanup.remove_trailing_whitespaces=true' in jdtUiPrefs and 'sp_cleanup.remove_trailing_whitespaces_ignore_empty=false' in jdtUiPrefs def settings(self): with open(self.path) as fp: - return fp.read() + java(self.javaCompliance).java + return fp.read() + java(self.javaCompliance).java + str(self.removeTrailingWhitespace) class FileInfo: def __init__(self, path): @@ -1965,13 +1970,25 @@ self.content = fp.read() self.times = (os.path.getatime(path), os.path.getmtime(path)) - def update(self): + def update(self, removeTrailingWhitespace): with open(self.path) as fp: content = fp.read() + + if self.content != content: + # Only apply *after* formatting to match the order in which the IDE does it + if removeTrailingWhitespace: + content, n = re.subn(r'[ \t]+$', '', content, flags=re.MULTILINE) + if n != 0 and self.content == content: + # undo on-disk changes made by the Eclipse formatter + with open(self.path, 'w') as fp: + fp.write(content) + if self.content != content: self.diff = difflib.unified_diff(self.content.splitlines(1), content.splitlines(1)) self.content = content return True + + # reset access and modification time of file os.utime(self.path, self.times) modified = list() @@ -1981,7 +1998,7 @@ continue sourceDirs = p.source_dirs() - batch = Batch(join(p.dir, '.settings', 'org.eclipse.jdt.core.prefs'), p.javaCompliance) + batch = Batch(join(p.dir, '.settings'), p.javaCompliance) if not exists(batch.path): if _opts.verbose: @@ -2010,7 +2027,7 @@ '-config', batch.path] + [f.path for f in batch.javafiles]) for fi in batch.javafiles: - if fi.update(): + if fi.update(batch.removeTrailingWhitespace): modified.append(fi) log('{0} files were modified'.format(len(modified)))