From 82aa60a838f9873c0c6dea94dcabe803bb9d185a Mon Sep 17 00:00:00 2001
From: Anton Leherbauer <anton.leherbauer@windriver.com>
Date: Thu, 31 Mar 2011 14:08:24 +0000
Subject: [PATCH] Bug 341086 - Add a variable like 'base_filename' to the code
 template variables

---
 .../corext/template/c/TemplateMessages.java    |  3 ++-
 .../template/c/TemplateMessages.properties     |  5 +++--
 .../template/c/TranslationUnitContextType.java | 18 ++++++++++++++----
 3 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TemplateMessages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TemplateMessages.java
index 01fb3d073ab..fa3b10f93a5 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TemplateMessages.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TemplateMessages.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2005, 2008 IBM Corporation and others.
+ * Copyright (c) 2005, 2011 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -23,6 +23,7 @@ public final class TemplateMessages extends NLS {
 	}
 
 	public static String CContextType_variable_description_file;
+	public static String CContextType_variable_description_file_base;
 	public static String CContextType_variable_description_enclosing_method;
 	public static String CContextType_variable_description_enclosing_project;
 	public static String CContextType_variable_description_enclosing_method_arguments;
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TemplateMessages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TemplateMessages.properties
index 4eadbefd074..b4401be3b06 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TemplateMessages.properties
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TemplateMessages.properties
@@ -1,5 +1,5 @@
 #########################################
-# Copyright (c) 2005, 2008 IBM Corporation and others.
+# Copyright (c) 2005, 2011 IBM Corporation and others.
 # All rights reserved. This program and the accompanying materials
 # are made available under the terms of the Eclipse Public License v1.0
 # which accompanies this distribution, and is available at
@@ -11,7 +11,8 @@
 #     Anton Leherbauer (Wind River Systems)
 #########################################
 
-CContextType_variable_description_file=Filename of translation unit
+CContextType_variable_description_file=Name of source file
+CContextType_variable_description_file_base=Name of source file without extension
 CContextType_variable_description_enclosing_method=Enclosing method name
 CContextType_variable_description_enclosing_project=Enclosing project name
 CContextType_variable_description_enclosing_method_arguments=Argument names of enclosing method
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TranslationUnitContextType.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TranslationUnitContextType.java
index 54852cbf3cc..bde96e49d4e 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TranslationUnitContextType.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TranslationUnitContextType.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2010 IBM Corporation and others.
+ * Copyright (c) 2000, 2011 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -13,6 +13,7 @@
 
 package org.eclipse.cdt.internal.corext.template.c;
 
+import org.eclipse.core.runtime.Path;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.Position;
 import org.eclipse.jface.text.templates.GlobalTemplateVariables;
@@ -61,11 +62,21 @@ public abstract class TranslationUnitContextType extends TemplateContextType {
 		@Override
 		public String resolve(TemplateContext context) {
 			ITranslationUnit unit= ((TranslationUnitContext) context).getTranslationUnit();
-			
 			return (unit == null) ? null : unit.getElementName();
 		}
 	}
 
+	protected static class FileBase extends TemplateVariableResolver {
+		public FileBase() {
+			super("file_base", TemplateMessages.CContextType_variable_description_file_base);  //$NON-NLS-1$
+		}
+		@Override
+		public String resolve(TemplateContext context) {
+			ITranslationUnit unit= ((TranslationUnitContext) context).getTranslationUnit();
+			return (unit == null) ? null : new Path(unit.getElementName()).removeFileExtension().lastSegment();
+		}
+	}
+
 	protected static class EnclosingCElement extends TemplateVariableResolver {
 		protected final int fElementType;
 		
@@ -186,6 +197,7 @@ public abstract class TranslationUnitContextType extends TemplateContextType {
 		
 		// translation unit
 		addResolver(new File());
+		addResolver(new FileBase());
 		addResolver(new ReturnType());
 		addResolver(new Method());
 		addResolver(new Project());
@@ -196,5 +208,3 @@ public abstract class TranslationUnitContextType extends TemplateContextType {
 	public abstract TranslationUnitContext createContext(IDocument document, int offset, int length, ITranslationUnit translationUnit);
 	public abstract TranslationUnitContext createContext(IDocument document, Position position, ITranslationUnit translationUnit);
 }
-
-