How to disable strange Android Studio comment indentation? - android-studio

I'm seeing a strange thing happening in Android Studio. This is with Kotlin. In an if block, if there is a comment before a return statement, and I hit enter at the end of the comment to presumably write some code (or even another comment), it indents as if it's a new block:
fun someFun() {
if (true) {
// Some comment
// Wants me to indent???
// And again...
// And again...
return
}
}
It's driving me up the wall and I can't figure out why this behavior is happening or how to disable it. If the return statement isn't there, this doesn't happen. It also only happens inside an if block. Any help would be much appreciated.

Related

Kotlin, Android, how to debug coroutines correctly?

I'm trying to debug my coroutines, and breakpoints placed into suspend function don't work. Pls help me understand why.
Working with Android Studio.
Ok, I launch a coroutine from viewModelScope:
viewModelScope.launch(IO) {
when(val result = interactor.getAllWords()){...}
}
In getAllWords() I wrote:
override suspend fun getAllWords(): WordResult {
val words = mutableListOf<Word>()
when (val wordsResult = getAllWordsWithoutFiltersApplying()) {}
...
return getWordsWithSelectedPattern()
I have two suspend functions: getAllWordsWithoutFiltersApplying() and getWordsWithSelectedPattern(). I have a breakpoints into both of them, but they did't trigger in debug mode.
At the same time, line val words = mutableListOf<Word>() is triggering, when I put breakpoint to its line.
And, if I put some log stuff into "untracing" function, they will be work. I say it to make it clear, suspend function works. Breakpoints are not.
What should I do to debug them?
*Screenshot added. Look at the left side with row of icons. Why my lines are not available?
Based on your sample code, you switch the coroutine context between MAIN and IO so when you set the breakpoint, make sure the suspend option is ALL
To show the option of the breakpoint. Set a breakpoint with the left click of your mouse, and then right click your mouse on the breakpoint.
If you are using the JetBrain IDE, according to the document, when you set the breakpoint to make sure the suspend option is ALL not thread. it works for me.
and more detail you can check the document
It still does not work for me because I run the app first then attach the debugger, but when I use debug instead, it works.
I know I'm late, but this is for those people who made similar mistakes like me. I've also faced this issue recently and the root cause was related to dependencies. Actually I added coroutine core dependency but I forget to add coroutine android dependency. Please make sure both dependencies are present in your gradle file as shown below.
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.4"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.4'
I had forgot minifyEnabled set to true in my build.gradle file
buildTypes {
release {
minifyEnabled true
}
debug {
minifyEnabled true <- Should be false for these breakpoints to work
}
}

Why is require-atomic-updates found here?

The help text for require-atomic-updates talks exclusively about statements that both set and consume the same variable.
I have some old† code that looks something like this (I think I've included everything that is relevant):
var someFunction = async function someFunction () {
switch(someVariable) {
case 0:
if (maybe) {
await doSomething();
}
break;
case 1:
//similar to above
}
someVariable = 0; // Error detected on this line
return
}
var someVariable = 0;
someFunction is invoked during some event processing later, while someVariable can be adjusted by multiple code paths
As far as I can tell, the line on which the error is reported is an atomic update, it doesn't read the value or set the new value based on anything else.
I can't understand why eslint thinks there is a possible race-condition here?
The code has been functional for a long time now, so I'm happy to just disable the rule on this line to stop it complaining. But I'd like to understand the reason that eslint highlighted it.
† The original code was written long ago, but has been adjusted more recently to be async
If you upgraded to eslint 6.0.1 like I just did, you're encountering a recently introduced bug.
There are several open github issues referencing this bug, but the gist of it is that require-atomic-updates is currently broken.
I recommend downgrading eslint or disabling the rule as a workaround.
Bug reports on the issue here:
https://github.com/eslint/eslint/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+require-atomic-updates

Program Just started getting tons of stack overflows with a whole new level of error

Alright, this is probably the biggest program i have ever written. I use task in it once so that on first run it will search all available files and folders for certain things. Everything works...well, did work. Now when i start the program after adding a few more features in. It is throwing stack overflow errors on things that never had a problem before.
My first one was right in the begging it threw on a simple return of,
return System.Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments)
i changed it to just return
public string BaseUpdaterPath { get { return "C:\\Users\\Public\\Documents"; } }
and it started working again.
it got alittle further into the program untill it runs a check to see if a specific path exists.
if (File.Exists(pathINI))
pathini is defined earlier as
string pathINI = (BaseProductPath + "\\" + Name + ".ini");
and baseproduct is
public string BaseProductPath { get { return BaseUpdaterPath + "\\" + Name; } }
these are things that should not be breaking. My error is this
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
with more investigation turns into this
Source Evaluation of method System.Exception.get_Source requires calling method System.Reflection.RuntimeMethodInfo.CreateDelegate, which cannot be called in this context. string
I can only imagine this has something to do with threading and the initial search that I am doing if this is the programs first run time.
If i can provide anymore information that i may have overlooked in my details i would be happy to provide it. I have not seen errors like this before that don't really give you much to go off of.
I may just end up throwing up a splash screen during that initial search if it's going to keep causing problems like this and have the rest of the program wait on that task.
UPDATE
I just stepped through my program starting at different steps and i was able to get past that check it kept kicking it out at. Then i got to a different if(foo == bar) and visual studio did this strange thing where it popped up a little loading window and said evaluating BAR then killed the program with
has exited with code -2147023895 (0x800703e9).
wtf is going on lmao
Another update incase anyone runs into this. I have a product preselected when i started up the programming, i wrote checks in to accommodate this however....it blocked me from being able to see what is really happening.
I disabled some code and let the thing run and after a while i found this little bastard popping up on the console
Exception thrown: 'System.IO.PathTooLongException' in mscorlib.dll
Exception thrown: 'System.IO.PathTooLongException' in mscorlib.dll
Exception thrown: 'System.IO.PathTooLongException' in mscorlib.dll
Exception thrown: 'System.IO.PathTooLongException' in mscorlib.dll
now i gotta track that down to wherever the hell it's throwing 5000 times and i should be good to go.
Update again if anyone cares.
I found out that even i caught and threw the exception away it was still getting stackoverflow in windows.old because they have tons of folders and files and i was running out of memory. My solution at this point is during the recursive file check to just discard anyfolder in windows.old. Cant think of a better way.
THE EPIC CONCLUSION...I AM A DUMBASS
i was having two properties check eachother
public bool IsInstalled { get
{
if (DefaultInstalledpath != null || DefaultInstalledpath != "Not Installed")
{
return true;
}
else
{
return false;
}
}
public string DefaultInstalledpath
{
get
{
else if(!File.Exists(pathTXT) && IsInstalled == true)
{
return "Discovering! Please Wait...";
}
.........
i don't wanna talk about the rabbit hole i just fell down

Simple Sleep() not working in VC++

I am working on a project than involves making an image appear and then disappear after a fixed time. I thought that the correct function for that purpose would be Thread::Sleep() but it doesn't seem to work.
this->pictureTConf->Visible = true;
Thread::Sleep(5000);
this->pictureTConf->Visible = false;
With this code, the picture doesn't appear at any moment. Any thoughts? Thanks.
PD: Using Visual Studio, Windows forms and VC++.
Setting a member variable to true isn't enough because your call to Sleep() prevents the code from running the message loop. Try this instead: (This is just for sample purposes, you shouldn't do this in a "real" application.)
this->pictureTConf->Visible = true;
this->pictureTConf->Refresh();
Thread::Sleep(5000);
this->pictureTConf->Visible = false;
this->pictureTConf->Refresh();
Also, as Chuck pointed out, if you use a timer, you shouldn't need the calls to Refresh(). Everything will just work.

Scala swing panel disappears when trying to change contents (only when running a Thread)

So I'm writing a boid simulation program as a project for school. My program supports multiple different groups of these boids that don't flock with other groups, they all have different settings which I do by adding a BoxPanel to the main GUI of the program when a new tribe is made, and those BoxPanels have a settings button that opens a new frame with the groups settings.
This works perfectly when I start the program up and add all the pre defined tribes that are in the code. Now I made a new part of the GUI that let's you make new groups of these boids and adds them while the simulation is running, and here is when the problems start for me.
For some weird reason it adds the group just fine, with the right settings in to the simulation but it wont add the BoxPanels to the main GUI. It makes the whole settings bar that I have in the side of my simulation disappear completely. I tested this out and if I add the tribes in the beginning of my calculation thread it does the same thing, so this seems to be a problem with multiple threads and swing. Any ideas what is causing this or how to fix this? I am completely perplexed by this.
tl;dr: The code below for adding tribes works fine when I haven't started the thread but if I try to use it after starting the thread the optionPanel appears empty.
Here's the code that adds the BoxPanels to the main gui:
def addTribe(tribe: Tribe) = {
tribeFrames += new TribeSettingFrame(tribe)
tribeBoxPanels += new TribeBoxPanel(tribe)
this.refcontents
}
private def refcontents = {
top.optionPanel.contents.clear()
top.optionPanel.contents += new BoxPanel(Orientation.Vertical) {
tribeBoxPanels.foreach(contents += _.tribeBoxPanel)
}
top.optionPanel.contents += new BoxPanel(Orientation.Horizontal) {
contents += top.addTribeButton
}
top.optionPanel.contents += new BoxPanel(Orientation.Horizontal) {
contents += top.vectorDebugButton
}
}
new Thread(BoidSimulation).start()
Oh and I tested if it really adds the contents that it should by printing out the sizes of the contents, and everything matches fine, it just won't draw them.
EDIT: After some digging around it really seems to be a thing with updating swing from a Thread. A lot of places suggest to use SwingWorker but from the info I gathered about it I don't think it would fit in my program since it is a continuous simulation and and I would have to keep making new SwingWorkers every frame.
EDIT2: Tried calling the method from the thread like this:
SwingUtilities.invokeLater(new Runnable() {
override def run() {
GUI2D.addTribe(tribe)
}
});
Didn't make any difference. I am starting to think that this is a problem with how I use TribeBoxPanel and TribeSettingFrame. These are objects that both contain only one method that returns the wanted BoxPanel or Frame. Is this implementation bad? If so what is the better way of creating dynamic BoxPanels and Frames?
Swing is not thread-safe.
Repeat after me.
Swing is not thread-safe.
Hear the chorus? Swing is not thread safe There is official documentation.
There is a very simple workaround given as well.
SwingUtilities.invokeLater(new Runnable() {
#Override public void run() {
// your stuff
}
});
In Scala, this is supported as:
Swing.invokeLater(/* your stuff */)
First you should let the UI thread handle all UI manipulation.
The simple way should be following Scala-Code:
Swing.onEDT { GUI2D.addTribe(tribe) }
But as you already noted, this won't solve your problem. I had a very similar problem where I only changed the text content of a Swing.Label and it sometimes simply disappeared.
It turned out that it only disappeared, when the text was too long to display it inside the Area which the Label initially reserved for itself. So one way around your Problem could be to give the optionPanel a bigger initial size when you create it.
Swing.onEDT { top.optionPanel.preferredSize = new Dimension(width,height) }
I'm not quite sure whether this has to be set before the component is first drawn (before Frame.open() is called).

Resources