/*
 * SnippetType.java
 *
 * Copyright (C) 2009-15 by RStudio, Inc.
 *
 * Unless you have received this program directly from RStudio pursuant
 * to the terms of a commercial license agreement with RStudio, then
 * this program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */
package org.rstudio.studio.client.workbench.snippets.ui;

import org.rstudio.studio.client.common.filetypes.TextFileType;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;

public class EditableSnippets extends Composite
{
   public EditableSnippets(TextFileType fileType)
   {
      this(fileType.getLabel(), fileType);
   }
   
   public EditableSnippets(String name, TextFileType fileType)
   {
      fileType_ = fileType;
      HorizontalPanel panel = new HorizontalPanel();
      Image icon = new Image(fileType.getDefaultIcon());
      icon.getElement().getStyle().setMarginRight(2, Unit.PX);
      panel.add(icon);
      Label label = new Label(name);    
      label.getElement().getStyle().setMarginLeft(5, Unit.PX);
      panel.add(label);
      initWidget(panel);
   }
   
   public String getEditorMode()
   {
      return fileType_.getEditorLanguage().getModeName();
   }
   
   public String getFileTypeLabel()
   {
      return fileType_.getLabel();
   }
   
   public String getSnippetText()
   {
      // if we haven't yet been edited then get the default snippet text
      if (pendingEdits_ != null)
      {
         return pendingEdits_;
      }
      else
      {
         return getSnippetText(getEditorMode());
      } 
   }
    
   public void recordPendingEdits(String pendingEdits)
   {
      pendingEdits_ = pendingEdits;
   }
   
   public void setScrollPosition(int scrollPosition)
   {
      scrollPosition_ = scrollPosition;
   }
   
   public int getScrollPosition()
   {
      return scrollPosition_;
   }
   
   public String getPendingEdits()
   {
      return pendingEdits_;
   }
   
   private static native String getSnippetText(String mode) /*-{
      // Try to get RStudio custom snippets first; if that fails,
      // then get Ace snippets.
      var rsSnippetId = "rstudio/snippets/" + mode;
      var snippetText = $wnd.require(rsSnippetId).snippetText;
      if (snippetText != null)
         return snippetText;
         
      // Fall back to Ace snippets
      var snippetId = "ace/snippets/" + mode;
      return $wnd.require(snippetId).snippetText;
   }-*/;
   
   private final TextFileType fileType_;
   private String pendingEdits_ = null;
   private int scrollPosition_ = 0;
}


@@ -2613,20 +2613,17 @@
  snippet
-Text
+s
  = $wnd.
@@ -2638,36 +2638,24 @@
 rsSnippetId)
-.snippetText
 ;%0A      if (
@@ -2665,20 +2665,33 @@
 ppet
-Text != null
+s && snippets.snippetText
 )%0A  
@@ -2704,16 +2704,25 @@
  return 
+snippets.
 snippetT
@@ -2823,22 +2823,30 @@
 ;%0A      
-return
+var snippets =
  $wnd.re
@@ -2861,28 +2861,125 @@
 ippetId)
-.snippetText
+;%0A      if (snippets && snippets.snippetText)%0A         return snippets.snippetText;%0A         %0A      return %22%22
 ;%0A   %7D-*

fix failure to load snippets for non-R/C++ modes