Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BF: converts EOL on opening and compiling scripts to Coder. #2278

Merged
merged 3 commits into from Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions psychopy/app/builder/builder.py
Expand Up @@ -53,6 +53,7 @@
from psychopy.projects import pavlovia

from psychopy.scripts import psyexpCompile
from psychopy.app.coder import BaseCodeEditor

canvasColor = [200, 200, 200] # in prefs? ;-)
routineTimeColor = wx.Colour(50, 100, 200, 200)
Expand Down Expand Up @@ -2311,6 +2312,10 @@ def compileScript(self, event=None):
self.app.showCoder() # make sure coder is visible
self.app.coder.fileNew(filepath=fullPath)
self.app.coder.fileReload(event=None, filename=fullPath)
# Convert EOL of currentDoc based on prefs
EOL = BaseCodeEditor.getEOL(self.app.prefs.coder['newlineConvention'])
if EOL is not None:
self.app.coder.currentDoc.ConvertEOLs(EOL)

def generateScript(self, experimentPath, target="PsychoPy"):
"""Generates python script from the current builder experiment"""
Expand Down
23 changes: 5 additions & 18 deletions psychopy/app/builder/dialogs/dlgsCode.py
Expand Up @@ -283,7 +283,11 @@ def __init__(self, parent, ID, prefs,
# 4 means 'tabs are bad'; 1 means 'flag inconsistency'
self.SetProperty("tab.timmy.whinge.level", "4")
self.SetViewWhiteSpace(self.prefs.appData['coder']['showWhitespace'])
self.SetViewEOL(False)
self.SetViewEOL(self.prefs.appData['coder']['showEOLs'])
# Set EOL mode of editor from prefs
EOL = self.getEOL(self.prefs.coder['newlineConvention'])
if EOL is not None:
self.SetEOLMode(EOL)

self.Bind(wx.stc.EVT_STC_MARGINCLICK, self.OnMarginClick)
self.SetIndentationGuides(False)
Expand Down Expand Up @@ -413,20 +417,3 @@ def OnMarginClick(self, evt):
self.Expand(lineClicked, True, True, 100)
else:
self.ToggleFold(lineClicked)

def Paste(self, event=None):
dataObj = wx.TextDataObject()
clip = wx.Clipboard().Get()
clip.Open()
success = clip.GetData(dataObj)
clip.Close()
if success:
txt = dataObj.GetText()
if not constants.PY3:
try:
# if we can decode/encode to utf-8 then all is good
txt.decode('utf-8')
except:
# if not then wx conversion broke so get raw data instead
txt = dataObj.GetDataHere()
self.ReplaceSelection(txt)
40 changes: 40 additions & 0 deletions psychopy/app/coder/codeEditorBase.py
Expand Up @@ -70,6 +70,28 @@ def __init__(self, parent, ID, pos, size, style):
def OnKeyPressed(self, event):
pass

def Paste(self, event=None):
import sys
from pkg_resources import parse_version
from psychopy import constants

dataObj = wx.TextDataObject()
clip = wx.Clipboard().Get()
clip.Open()
success = clip.GetData(dataObj)
clip.Close()
if success:
txt = dataObj.GetText()
# dealing with unicode error in wx3 for Mac
if parse_version(wx.__version__) >= parse_version('3') and sys.platform == 'darwin' and not constants.PY3:
try:
# if we can decode from utf-8 then all is good
txt.decode('utf-8')
except Exception as err:
# if not then wx conversion broke so get raw data instead
txt = dataObj.GetDataHere()
self.ReplaceSelection(txt)

def HashtagCounter(self, text, nTags=0):
# Hashtag counter - counts lines beginning with hashtags in selected text
for lines in text.splitlines():
Expand Down Expand Up @@ -219,3 +241,21 @@ def indentSelection(self, howFar=4):
newIndent = 0
self.SetLineIndentation(lineN, newIndent)

@staticmethod
def getEOL(prefs='keep'):
"""Gets EOL mode from preferences for setting EOL mode in Coder and code component.
CRLF : Windows
CR : Legacy Mac
LF : Unix
None : Preferences are to be kept same.

Returns
-------
int
None, 0 = CRLF, 1 = CR, and 2 = LF
"""
EOL = {'keep': None,
'dos': 0,
'LegacyMac': 1,
'unix': 2}
return EOL[prefs]
29 changes: 10 additions & 19 deletions psychopy/app/coder/coder.py
Expand Up @@ -506,6 +506,10 @@ def __init__(self, parent, ID, frame,
self.coder = frame
self.SetViewWhiteSpace(self.coder.appData['showWhitespace'])
self.SetViewEOL(self.coder.appData['showEOLs'])
# Set EOL mode of editor from prefs
EOL = self.getEOL(self.coder.prefs['newlineConvention'])
if EOL is not None:
self.SetEOLMode(EOL)

self.Bind(wx.EVT_DROP_FILES, self.coder.filesDropped)
self.Bind(wx.stc.EVT_STC_MODIFIED, self.onModified)
Expand Down Expand Up @@ -955,24 +959,6 @@ def uncommentLines(self):
newText = newText + lineText
self._ReplaceSelectedLines(newText)

def Paste(self, event=None):
dataObj = wx.TextDataObject()
clip = wx.Clipboard().Get()
clip.Open()
success = clip.GetData(dataObj)
clip.Close()
if success:
txt = dataObj.GetText()
# dealing with unicode error in wx3 for Mac
if wx.__version__[0] == '3' and sys.platform == 'darwin':
try:
# if we can decode from utf-8 then all is good
txt.decode('utf-8')
except:
# if not then wx conversion broke so get raw data instead
txt = dataObj.GetDataHere()
self.ReplaceSelection(txt)

def increaseFontSize(self):
self.SetZoom(self.GetZoom() + 1)

Expand Down Expand Up @@ -2264,7 +2250,9 @@ def fileOpen(self, event=None, filename=None):
else:
self.setCurrentDoc(filename)
self.setFileModified(False)

EOL = BaseCodeEditor.getEOL(self.prefs['newlineConvention'])
if EOL is not None:
self.currentDoc.ConvertEOLs(EOL)
self.SetStatusText('')
# self.fileHistory.AddFileToHistory(newPath) # this is done by
# setCurrentDoc
Expand Down Expand Up @@ -2352,6 +2340,8 @@ def fileSave(self, event=None, filename=None, doc=None):
newlines = '\r\n'
elif self.prefs['newlineConvention'] == 'unix':
newlines = '\n'
elif self.prefs['newlineConvention'] == 'LegacyMac':
newlines = '\r'
except Exception:
pass

Expand Down Expand Up @@ -2651,6 +2641,7 @@ def paste(self, event):
foc = self.FindFocus()
if hasattr(foc, 'Paste'):
foc.Paste()
self.currentDoc.ConvertEOLs(BaseCodeEditor.getEOL(self.prefs['newlineConvention']))

def undo(self, event):
self.currentDoc.Undo()
Expand Down
2 changes: 1 addition & 1 deletion psychopy/preferences/Darwin.spec
Expand Up @@ -85,7 +85,7 @@
# for coder shell window, which shell to use
preferredShell = option('ipython','pyshell',default='pyshell')
# newline for python files: unix = \n, dos = \r\n
newlineConvention = option('keep','unix','dos',default='keep')
newlineConvention = option('keep','unix','dos','LegacyMac',default='keep')

# Settings for the Builder window
[builder]
Expand Down
2 changes: 1 addition & 1 deletion psychopy/preferences/FreeBSD.spec
Expand Up @@ -85,7 +85,7 @@
# for coder shell window, which shell to use
preferredShell = option('ipython','pyshell',default='pyshell')
# newline for python files: unix = \n, dos = \r\n
newlineConvention = option('keep','unix','dos',default='keep')
newlineConvention = option('keep','unix','dos','LegacyMac',default='keep')

# Settings for the Builder window
[builder]
Expand Down
2 changes: 1 addition & 1 deletion psychopy/preferences/Linux.spec
Expand Up @@ -85,7 +85,7 @@
# for coder shell window, which shell to use
preferredShell = option('ipython','pyshell',default='pyshell')
# newline for python files: unix = \n, dos = \r\n
newlineConvention = option('keep','unix','dos',default='keep')
newlineConvention = option('keep','unix','dos','LegacyMac',default='keep')

# Settings for the Builder window
[builder]
Expand Down
2 changes: 1 addition & 1 deletion psychopy/preferences/Windows.spec
Expand Up @@ -85,7 +85,7 @@
# for coder shell window, which shell to use
preferredShell = option('ipython','pyshell',default='pyshell')
# newline for python files: unix = \n, dos = \r\n
newlineConvention = option('keep','unix','dos',default='keep')
newlineConvention = option('keep','unix','dos','LegacyMac',default='keep')

# Settings for the Builder window
[builder]
Expand Down
2 changes: 1 addition & 1 deletion psychopy/preferences/baseNoArch.spec
Expand Up @@ -81,7 +81,7 @@
# for coder shell window, which shell to use
preferredShell = option('ipython','pyshell',default='pyshell')
# newline for python files: unix = \n, dos = \r\n
newlineConvention = option('keep','unix','dos',default='keep')
newlineConvention = option('keep','unix','dos','LegacyMac',default='keep')

# Settings for the Builder window
[builder]
Expand Down