What are "Code Styles" in Intelli J IDEs? - android-studio

I took a look at this, and I thought it was a color scheme or something. I followed my stems to find out that it's not.
What are "Code Styles" and what changes can they, for example, do?

Code styles dictate what happens when you auto-format your code, which includes:
The placing of braces, forcing or omitting their inclusion with a single statement
The amount of spaces indentation does
Whether or not you chop down or wrap long statements or parameters
How annotations appear in fields (on top of/next to)
Whether or not you use import * statements in Java, and what/when the cutoff is
How you close ML tags (XML, HTML, etc - either with the </full-name> or <full-name />)
...and many other pieces.
Code style varies from person to person, and shop to shop, so having IntelliJ as a way to configure this once and shared with others is ideal.

Related

Is there any reason to reference UI element text in the strings.xml resource file rather than hard-coding in Android Studio?

It seems like it's simply more straightforward to hard-code the text values. In an event that these values should be changed it seems like it would be more logical to search for the relevant UI element in each activity's xml layout file rather than look through the entire strings.xml. Of course if you have certain UI elements across multiple activities that all share the same text then this might be an exception (like a back button for instance), but generally there doesn't seem to be much advantage to storing these in the strings.xml. Am I missing something?
I will give you two reasons;
1 - Avoid duplication: all of your strings in one place. also, you can use string value many times. when you want to change it, there is one place to do the change. that makes it easier to maintain.
2 - Multi-language support: if you want to translate your strings to another language you must have all the strings in Strings.xml
let me know if you need more clarifications.

Writing an autocomplete plugin in Sublime Text

Within my company we have an XML-based notation. Among other features, it is possible to define references from one XML document into another. I would like to enable autocompletion in Sublime so that whenever I am adding a reference, the possible files (i.e. XML files within the same project) and link points (i.e. symbols within that file) get offered as recommendations.
So far, I have found a lot of plugins that enable autocomplete for, say, HTML, PHP or LaTeX. However, I have the feeling the code base is too complex for a somewhat simple task. Is there, for instance, some vanilla function that generates completions based on an arbitrary array received as parameter? I would create the logic to determine what is a symbol and derive said array, but the whole process seems somewhat cumbersome to me.
(As a note: I can program in Python and have fiddled with other Sublime features, such as snippets, but these packages seem to be much more complex than it feels necessary.)
The base to create the completions entry is not to complicated. You now need to fill the array with the correct values (this could be done via a project setting or parsing other files).
import sublime
import sublime_plugin
# Your array, which contains the completions
arr = ["foo", "bar", "baz"]
class MyCompletionsListener(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
loc = locations[0]
# limit you completions scope
if not view.score_selector(loc, "text"):
return
completions = [(v + "\tYour Description", v) for v in arr]
return completions
OP's note: The answer works as advertised. However, the integration is so seamless that I thought for a while that something was missing. If the Python script above is on the right folder, all of the completions returned by the completions array will be suggested (depending on Sublime settings, it might be necessary to trigger the completions menu with Ctrl+Space). Also worth noting:
The completions may be None, in which case they just don't add any completion option, or an array of 2-tuples, where the first element is the description (which will be shown in the drop-down menu and trigger the completion) and the second is the value (i.e. the text that will be input if the completion is selected).
The score_selector method can be used to determine if the cursor position is within a given scope.

Exclude comments from search results in IntelliJ global search?

I found the grammar error "it's" as a possessive on one page of a large project. I'm trying to search for any other usages of this on pages to correct it, but I'm getting results containing hundreds of comments. I just want to filter for the important user-facing portions of the project. Is there a way to exclude comments from the results of a global search?
In more recent versions, at least in PyCharm 2018 (similar to IntelliJ), there is a filter option "Except comments," as shown here:
(Click the small filter icon to show the dropdown.)
Note: The selected filter option persists during a session, and the active filter option is not immediately apparent unless you open the dropdown. To prevent accidentally limiting subsequent searches, it may be a good idea to switch back to "Anywhere" afterward.
Another approach would be to enable the "Regular expression" (or "Regex") checkbox in the search dialog, then use some kind of negative lookaround to exclude comments.
In one case, I needed to exclude lines with single-line comments (e.g. # this is a comment) from a search, but not lines with inline comments (e.g. a=b+1 # this is an inline comment). The following did the trick, searching for something (for Python comments, starting with #):
^((?!#).)*something.*$
Please note I'm not a regex-expert, so this regex pattern can probably be improved upon greatly, but it illustrates the idea. You can play around with this on regex101. Any comments to improve the pattern are most welcome, of course.
Note sure if this approach could be extended to multiline comments though. (as in """ several lines here """).
It's difficult to suggest something without really looking at the code, but since it seems like a one-off thing, I would use global search just in comments to temporary replace "it's" with some #temporary-token#, then use global search everywhere, you should everything what's left. Then rollback temporary token for comments. Should be easy to try with VCS. Just an idea.
As you can see, with "Comments only" option, only one #token is found.

creating tags for a script language for easy browsing in vim

I use ctags+Vim for a lot of my projects and I really like the ability to easily browse through large chunks of code quickly.
I am also using Stata, a statistical package, which has a script language. Even though you can have routines in the code, its code tends to be series of commands that perform data and statistics operations. And the code files can be very long. So I always find myself in need of a way to browse it efficiently.
Since I use Vim, I can use marks. But I was wondering if I could use ctags to do this. That is, I want to create a tag marker which (1) won't cause a problem when I run the script (2) easy to introduce to ctags.
Because it is supposed to not break the script, it needs to be a comment. In Stata, comment lines start with * and flow comments can be made by /* ..... */.
It would be great, for example, have sections in the code, marked by comments:
* Section: Data
And ctags picks up "Data Manipulation" as the tag. So I can see a list of sections and jump to them easily without the needs for creating marks.
Is there anyway to do this? I'd appreciate any comments.
You need a way to generate a tags database for your Stata files. The format is simple, see :help tags-file-format. The default tags program, Exuberant Ctags can be extended with regular expressions (--langmap, --regex); that probably only yields an approximate parsing for complex languages, but it should suffice for custom section marks; maybe you could even directly extract interesting language keywords.

Managing line width in ColdFusion

If you work with ColdFusion, you've probably gotten used to either line-wrapping or horizontal scrolling. Infrequently, this isn't so bad, but when three out of four lines in an existing code base are wrapped, sometimes twice, it becomes a huge readability hindrance.
A line such as...
<cffunction name="v_multiple_item" access="public" output="false" returntype="query">
can easily be broken up HTML-like into
<cffunction name = "v_multiple_item"
access = "public"
output = "false"
returntype = "query"
>
Which is fairly easy to automate with a macro in a good editor. What I'm more concerned with is interspersed ColdFusion and HTML, like so:
<cffunction..>
<cfif..>
<cfif..>
<form..>
<div..>
<table..>
<tr..>
<td..>
<cfif..>
<select..>
<cfoutput>
<option>#stuff#</option>
</cfoutput>
It's not uncommon that I see lines of code, with 8-sp tabs, that are line-wrapped during the whitespace.
Reducing tab width hasn't been enough. The code base is too big for rewrites/frameworkification to be an option. Left-justify everything?
Is there a simple, winning strategy for reducing line widths to a manageable level without further damaging readability?
I don't want to get into a 'tabs versus spaces' issue here, but one thing you can do is adjust your tabs in the IDE. If you use tabbing, you could always make a tab be 2 or 3 spaces rather than 8 (or more). This will reduce the unnecessary whitespace, at least visually.
In Eclipse you can do this under Preferences > General > Editors > Text Editors. There's a 'Displayed Tab Width' there which you can reduce to your heart's content.
The real answer, however, is that this is just the nature of the beast. If you're going to intermix CF with HTML, and you want clarity, you'll indent your code.
Edit: Come to think of it, CFInclude just came to mind. Granted, this can also cause spaghetti code, but how things like this are often handled is with CFInclude. You can break reusable portions of CF code into cfm templates and include those. For a situation with a header, with many nested divs and pieces of CF logic intermixed, you'd be better off using a cfinclude anyway. So an index.cfm page that looked like your example might instead look more like this:
<cfinclude template="header.cfm">
<div>Some Content Here</div>
<cfinclude template="footer.cfm">
All of your wrapping is included in header.cfm or footer.cfm. You can even do includes within those templates as well, keeping things like the navigation isolated into a separate cfm page as well.
I'm not advocating that you go hogwild with includes, but it's a standard way of handling reuseable snippets of HTML/CFML in ColdFusion, and breaks things up into conceptual blocks that makes it easier to find what you're looking for.
IMO, this isn't a ColdFusion specific problem, this is related to code readability in general.
My solution? I got a 28" monitor. :)

Resources