Mercurial > hg > graal-jvmci-8
comparison mxtool/mx.py @ 14874:7dfc0e9fd45a
added removal of trailing whitespace to eclipseformat to emulate the actions performed by the IDE
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Fri, 28 Mar 2014 14:58:35 +0100 |
parents | 66ac13a2c7a1 |
children | 7683ad455d81 |
comparison
equal
deleted
inserted
replaced
14873:00eb80d735ed | 14874:7dfc0e9fd45a |
---|---|
1947 projects = sorted_deps() | 1947 projects = sorted_deps() |
1948 if args.projects is not None: | 1948 if args.projects is not None: |
1949 projects = [project(name) for name in args.projects.split(',')] | 1949 projects = [project(name) for name in args.projects.split(',')] |
1950 | 1950 |
1951 class Batch: | 1951 class Batch: |
1952 def __init__(self, settingsFile, javaCompliance): | 1952 def __init__(self, settingsDir, javaCompliance): |
1953 self.path = settingsFile | 1953 self.path = join(settingsDir, 'org.eclipse.jdt.core.prefs') |
1954 self.javaCompliance = javaCompliance | 1954 self.javaCompliance = javaCompliance |
1955 self.javafiles = list() | 1955 self.javafiles = list() |
1956 with open(join(settingsDir, 'org.eclipse.jdt.ui.prefs')) as fp: | |
1957 jdtUiPrefs = fp.read() | |
1958 self.removeTrailingWhitespace = 'sp_cleanup.remove_trailing_whitespaces_all=true' in jdtUiPrefs | |
1959 if self.removeTrailingWhitespace: | |
1960 assert 'sp_cleanup.remove_trailing_whitespaces=true' in jdtUiPrefs and 'sp_cleanup.remove_trailing_whitespaces_ignore_empty=false' in jdtUiPrefs | |
1956 | 1961 |
1957 def settings(self): | 1962 def settings(self): |
1958 with open(self.path) as fp: | 1963 with open(self.path) as fp: |
1959 return fp.read() + java(self.javaCompliance).java | 1964 return fp.read() + java(self.javaCompliance).java + str(self.removeTrailingWhitespace) |
1960 | 1965 |
1961 class FileInfo: | 1966 class FileInfo: |
1962 def __init__(self, path): | 1967 def __init__(self, path): |
1963 self.path = path | 1968 self.path = path |
1964 with open(path) as fp: | 1969 with open(path) as fp: |
1965 self.content = fp.read() | 1970 self.content = fp.read() |
1966 self.times = (os.path.getatime(path), os.path.getmtime(path)) | 1971 self.times = (os.path.getatime(path), os.path.getmtime(path)) |
1967 | 1972 |
1968 def update(self): | 1973 def update(self, removeTrailingWhitespace): |
1969 with open(self.path) as fp: | 1974 with open(self.path) as fp: |
1970 content = fp.read() | 1975 content = fp.read() |
1976 | |
1977 if self.content != content: | |
1978 # Only apply *after* formatting to match the order in which the IDE does it | |
1979 if removeTrailingWhitespace: | |
1980 content, n = re.subn(r'[ \t]+$', '', content, flags=re.MULTILINE) | |
1981 if n != 0 and self.content == content: | |
1982 # undo on-disk changes made by the Eclipse formatter | |
1983 with open(self.path, 'w') as fp: | |
1984 fp.write(content) | |
1985 | |
1971 if self.content != content: | 1986 if self.content != content: |
1972 self.diff = difflib.unified_diff(self.content.splitlines(1), content.splitlines(1)) | 1987 self.diff = difflib.unified_diff(self.content.splitlines(1), content.splitlines(1)) |
1973 self.content = content | 1988 self.content = content |
1974 return True | 1989 return True |
1990 | |
1991 # reset access and modification time of file | |
1975 os.utime(self.path, self.times) | 1992 os.utime(self.path, self.times) |
1976 | 1993 |
1977 modified = list() | 1994 modified = list() |
1978 batches = dict() # all sources with the same formatting settings are formatted together | 1995 batches = dict() # all sources with the same formatting settings are formatted together |
1979 for p in projects: | 1996 for p in projects: |
1980 if p.native: | 1997 if p.native: |
1981 continue | 1998 continue |
1982 sourceDirs = p.source_dirs() | 1999 sourceDirs = p.source_dirs() |
1983 | 2000 |
1984 batch = Batch(join(p.dir, '.settings', 'org.eclipse.jdt.core.prefs'), p.javaCompliance) | 2001 batch = Batch(join(p.dir, '.settings'), p.javaCompliance) |
1985 | 2002 |
1986 if not exists(batch.path): | 2003 if not exists(batch.path): |
1987 if _opts.verbose: | 2004 if _opts.verbose: |
1988 log('[no Eclipse Code Formatter preferences at {0} - skipping]'.format(batch.path)) | 2005 log('[no Eclipse Code Formatter preferences at {0} - skipping]'.format(batch.path)) |
1989 continue | 2006 continue |
2008 'org.eclipse.jdt.core.JavaCodeFormatter', | 2025 'org.eclipse.jdt.core.JavaCodeFormatter', |
2009 '-vm', java(batch.javaCompliance).java, | 2026 '-vm', java(batch.javaCompliance).java, |
2010 '-config', batch.path] | 2027 '-config', batch.path] |
2011 + [f.path for f in batch.javafiles]) | 2028 + [f.path for f in batch.javafiles]) |
2012 for fi in batch.javafiles: | 2029 for fi in batch.javafiles: |
2013 if fi.update(): | 2030 if fi.update(batch.removeTrailingWhitespace): |
2014 modified.append(fi) | 2031 modified.append(fi) |
2015 | 2032 |
2016 log('{0} files were modified'.format(len(modified))) | 2033 log('{0} files were modified'.format(len(modified))) |
2017 | 2034 |
2018 if len(modified) != 0: | 2035 if len(modified) != 0: |