@@ -519,12 +519,12 @@ CloudPebble.Editor = (function() {
519519 var check_safe = function ( ) {
520520 return Ajax . Get ( '/ide/project/' + PROJECT_ID + '/source/' + file . id + '/is_safe?modified=' + file . lastModified ) . then ( function ( data ) {
521521 if ( ! data . safe ) {
522- if ( was_clean ) {
522+ if ( code_mirror . was_clean ) {
523523 code_mirror . setOption ( 'readOnly' , true ) ;
524524 return CloudPebble . Get ( '/ide/project/' + PROJECT_ID + '/source/' + file . id + '/load' , function ( data ) {
525525 code_mirror . setValue ( data . source ) ;
526526 file . lastModified = data . modified ;
527- was_clean = true ; // this will get reset to false by setValue.
527+ code_mirror . was_clean = true ; // this will get reset to false by setValue.
528528 } ) . finally ( function ( ) {
529529 code_mirror . setOption ( 'readOnly' , false ) ;
530530 } ) ;
@@ -552,24 +552,24 @@ CloudPebble.Editor = (function() {
552552 }
553553 } ,
554554 onDestroy : function ( ) {
555- if ( ! was_clean ) {
555+ if ( ! code_mirror . was_clean ) {
556556 -- unsaved_files ;
557557 }
558558 delete open_codemirrors [ file . id ] ;
559559 }
560560 } ) ;
561561
562- var was_clean = true ;
562+ code_mirror . was_clean = true ;
563563 code_mirror . on ( 'change' , function ( ) {
564- if ( was_clean ) {
564+ if ( code_mirror . was_clean ) {
565565 CloudPebble . Sidebar . SetIcon ( 'source-' + file . id , 'edit' ) ;
566- was_clean = false ;
566+ code_mirror . was_clean = false ;
567567 ++ unsaved_files ;
568568 }
569569 } ) ;
570570
571571 var mark_clean = function ( ) {
572- was_clean = true ;
572+ code_mirror . was_clean = true ;
573573 -- unsaved_files ;
574574 CloudPebble . Sidebar . ClearIcon ( 'source-' + file . id ) ;
575575 } ;
@@ -1284,6 +1284,18 @@ CloudPebble.Editor = (function() {
12841284 }
12851285 }
12861286
1287+ var get_active_source_file_id = function ( ) {
1288+ var pane_id = $ ( '#main-pane' ) . data ( 'pane-id' ) ;
1289+ if ( ! pane_id || pane_id . indexOf ( 'source-' ) !== 0 ) {
1290+ return null ;
1291+ }
1292+ var file_id = parseInt ( pane_id . substring ( 'source-' . length ) , 10 ) ;
1293+ if ( isNaN ( file_id ) ) {
1294+ return null ;
1295+ }
1296+ return file_id ;
1297+ } ;
1298+
12871299 return {
12881300 Create : function ( ) {
12891301 create_source_file ( ) ;
@@ -1311,6 +1323,37 @@ CloudPebble.Editor = (function() {
13111323 } ,
13121324 RenameFile : function ( file , new_name ) {
13131325 return rename_file ( file , new_name )
1326+ } ,
1327+ /**
1328+ * Re-fetch the source of the currently-active editor from the server and
1329+ * replace its buffer. No-op if the active pane isn't a source file, if
1330+ * the editor is currently detached, or if the buffer has unsaved local
1331+ * edits (we never clobber user work).
1332+ *
1333+ * Called from Sidebar.Refresh after a GitHub pull, so the user doesn't
1334+ * end up staring at a stale buffer for a file that just changed on
1335+ * the server.
1336+ */
1337+ ReloadActive : function ( ) {
1338+ var file_id = get_active_source_file_id ( ) ;
1339+ if ( file_id === null ) {
1340+ return Promise . resolve ( ) ;
1341+ }
1342+ var code_mirror = open_codemirrors [ file_id ] ;
1343+ if ( ! code_mirror ) {
1344+ return Promise . resolve ( ) ;
1345+ }
1346+ if ( ! code_mirror . was_clean ) {
1347+ return Promise . resolve ( ) ;
1348+ }
1349+ return Ajax . Get ( '/ide/project/' + PROJECT_ID + '/source/' + file_id + '/load' ) . then ( function ( data ) {
1350+ code_mirror . setValue ( data . source ) ;
1351+ code_mirror . was_clean = true ;
1352+ var file = project_source_files [ code_mirror . file_path ] ;
1353+ if ( file ) {
1354+ file . lastModified = data . modified ;
1355+ }
1356+ } ) ;
13141357 }
13151358 } ;
13161359} ) ( ) ;
0 commit comments