Smartparens
Minor mode for Emacs that deals with parens pairs
...You may want to try smartparens-strict-mode. This enforces that pairs are always balanced, so commands like kill-line keep your code well-formed. Inside Emacs, M-x sp-cheat-sheet will show you all the commands available, with examples. Smartparens has a default configuration that works well for most languages. For language-specific behaviour, smartparens has a separate smartparens-FOO.el file for every language and smartparens-config.el loads these automatically.